Sunday, 17 September 2017

If, Else and Comparing with ==

So far I've really only used one control structure (that is a part of the program that controls how the program flows) - the for loop. This is useful up to a point and previous script in this blog have demonstrated the for loop very well.
The if structure allows the computer to do stuff once depending on whether or not a statement is true rather than a number of times.
So a very quick script to show this might look like:

#!/usr/bin/python3
username = input("Hello, what is your name? ")
print("Hello ", username)
if username == "John":
    print ("Nice to have you back!")

  
A couple of points:
Something that often catches me out is that Python uses a single = for assigning values to variables, but when comparing to see if things are the same, it uses == (double =). Here because it is checking to see if the inputted string is the same as "John", it uses ==. Fortunately in Python (unlike some other languages), == is good for comparing both strings and numbers.
There is a colon after the comparison or the thing that should be true. This tells Python that the indented line(s) that follow the colon should only be run if the thing being checked is true.

So if I run it twice, giving it two different inputs, this is what we get:
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? Bob
Hello  Bob
>>>
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? John
Hello  John
Nice to have you back!
>>>

 But what if the comparison is false (i.e. not what the if statement is looking for), and you want the program to do something else? This is where else comes in. It is not used on its own, but immediately after an if statement, as an alternative set of instructions.
I can add a few lines to the script:
#!/usr/bin/python3
username = input("Hello, what is your name? ")
print("Hello ", username)
if username == "John":
    print ("Nice to have you back!")
else:
    print("I don't think we've met before.")
And so the output from two different runs of this script:
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? John
Hello  John
Nice to have you back!

>>>
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? Mike
Hello  Mike
I don't think we've met before.

>>>

 Note that like if, else also has a colon after it, and the alternative set of instructions are also indented. However, it does not have any comparison or checking if something is true or false - that is done in the preceding if statement.

There is a third option - elif. This checks a different comparison if the first one (in the if statement) is not true. The elif statement comes in between the if and the else statements, and you can have as many of them as you want. They form a cascade of checks to see which, if any, conditions apply.
Let's modify the script again.
#!/usr/bin/python3
username = input("Hello, what is your name? ")
print("Hello ", username)
if username == "John":
    print ("Nice to have you back!")
elif username =="Sara":
    print ("You're looking nice today!")
elif username =="Englebert":
    print("Is your second name Humperdink?")
elif username =="Algernon":
    print("Go away Algernon, you stupid idiot")
else:
    print("I don't think we've met before.")
So running the script a few times with different inputs gives the output
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? Englebert
Hello  Englebert
Is your second name Humperdink?

>>>
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? Algernon
Hello  Algernon
Go away Algernon, you stupid idiot

>>>
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? Alison
Hello  Alison
I don't think we've met before.

>>>
 In these control structures, indentation becomes very important, as it tells both Python and human readers whether a line is part of the control structure (i.e. linked to the if, else or elif statement preceding it) or not. This is the same principle as the for structure. Any lines after the if/elif/else statement that are not indented are run regardless of comparisons. For example:
#!/usr/bin/python3
username = input("Hello, what is your name? ")
print("Hello ", username)
if username == "John":
    print ("Nice to have you back!")
elif username =="Sara":
    print ("You're looking nice today!")
else:
    print("I don't think we've met before.")
print ("This line gets printed anyway!")
The last line is not indented, so does not belong to the else statement, and is run after Python has gone through the if/elif/else part. And if we run it several times with different inputs:
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? John
Hello  John
Nice to have you back!
This line gets printed anyway!

>>>
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? Sara
Hello  Sara
You're looking nice today!
This line gets printed anyway!

>>>
 RESTART: C:/Users/John/Dropbox/Misc Programming/Python/python3/test02_if.py
Hello, what is your name? Andy
Hello  Andy
I don't think we've met before.
This line gets printed anyway!

>>>


No comments:

Post a Comment