Search TekSocial
Stay Connected

Enter your email address:

(We respect your privacy!)

Or subscribe with your favorite RSS Reader

  

« Five Sites to Go to when you're bored | Main | Easy DIY Steps To VOIP Soft Phone Setup »
8:19PM

Programming with Python- If's Elif's Else's 

In todays video we go over the basic principle of the If, elif, and else function in python. The best way to explain this in summary is to say that if one action happens, then this action happens. Elif (Or else that doesn’t end.) this action will happen; else (which is the ending statement) this action happens. Every if has to have an else. With out else the function will not work properly. There is no easy way to explain all this in code unless I show you what it means in code. For example:  

 

We will ask the user how his day was.

 

print "How are you today?"

 

Now this next part may seem confusing.

answer = raw_input("< ")

 

What does that mean? Its another variation of a string theory. Meaning we are giving the word “answer” the meaning of a raw_inuput() (which is giving the person running the program a chance and opportunity to type in something.

 

if answer == "good":

            print "That's good to hear!"

 

elif answer == "bad":

            print "I'm sorry to hear that."

 

elif answer == "okay":

            print "thats better than bad"

 

else:

            print "Are you still there?"

 

Now this may seem odd at first. But in a nutshell what is going on is, if the raw_input equals the typed word “good” then it will print out the response. In this case the response is “That’s good to hear!”. Elif is kind of like else, but with an actual variable. So if the user types in “bad” or “okay” Then two separate responses will pop up to answer the user. Else (Meaning if they just press enter and nothing was typed in) it will print “Are you still there” meaning no answer was given.

 

Type in the code and give it a shot. This is another example of something we will be getting into the future. It will be a text based game much like zork. But there is still a bit more we have to get into before we get that far. I hope you found this helpful in some way, and thanks for watching.

 

For more videos, check us out at
http://www.youtube.com/user/thedeskgeek

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>