Python 101 (part 2): If Wishes Were Pythons - Tying Up The Loose Ends
(Page 7 of 9 )
In addition to the "if" statement, Python also allows you a couple of variants - the first is the "if-else" statement, which allows you to execute different blocks of code depending on whether the expression is evaluated as true or false.
The structure of an "if-else" statement looks like this:
if condition:
do this!
else:
do this!
In this case, if the conditional expression evaluates as
false, all statements within the indented "else" block will be executed. Modifying the example above, we have
#!/usr/bin/python
# ask for a number
alpha = input("Gimme a number! ")
# ask for another number
beta = input("Gimme another number! ")
# check
if alpha == beta:
print ("Can't you read, moron? I need two *different* numbers!")
else:
print ("Finally! A user with active brain cells!");
Next: Cookie-Cutter Code >>
More Python Articles
More By Vikram Vaswani, (c) Melonfire