HomePython Page 2 - Python Conditionals, Lists, Dictionaries, and Operators
Elif - Python
In our last article we learned how to get input from the user, store data in a variable, and work with some basic operators to manipulate that data. In this article we will learn to use Conditional Statements and possibly create functions. So wipe that mustard off your chin, clean the dishes, and let's get to work.
The Elif works like the Else If in other languages. It states that if the original If statement is False, and then the Elif part is true, to do this. Here it is in code:
beers = 100
while beers != 0:
beers = input (“How many beers are on the wall?”)
if beers >= 1 and beer <= 20:
print “Almost there. Take one down and pass it around!”
elif beers >=21 :
print “You got a lot of drinking to do!”
print “You should seek help!”
This program starts off creating a variable named beers. It then assigns the value 100 to it. Next, it creates a loop, that will continue looping while the value of beers is not equal to 0. Next it goes into an If statement that asks the user how many beers on are the wall. It then takes whatever numeric value the user enters and stores it in the beers variable. If the value is 0, it will exit the loop and print “You should seek help!” If the value is between 1 and 20 (greater than or equal to one AND less than or Equal to 20), it will print: “Almost there. Take one down and pass it around!” and continue through the loop. If the user enters a value equal to or greater than 21, it will print “You got a lot of drinking to do!” and once more continue through the loop.