More on elif
# Fortune program. More fun if you implement your own!
print(“I’m going to predict your fortune for today.”)
fortune = random.randint(1, 3) # 3 possible fortunes
if fortune == 1: # if fortune is equal to 1
print(“\n\tIn the mountains of truth you will never climb in vain.”)
elif fortune == 2: # else if the fortune is equal to 2
print(“\n\tIn the middle of winter I at last discovered that there was in me an invincible summer.”)
elif fortune == 3:
print(“\n\tThere can be no doubt that all our knowledge begins with experience.”)
print(“This should never occur, but demonstrates else being used defensively in case of error”)
These occur in real, mundane life. “While your hands are dirty: rinse with water. Lather with soap. Repeat.” Whilst your hands are dirty, the loop repeats, because dirty still evaluates to True. It breaks out of it once your hands are clean (dirty becomes False).
# Annoying program
print(“It seems you dont have anti-virus software installed!\n”)
response = “” # sentry variable
while response != “No.”:
response = input(“Install anti-virus software now!\n”)
print(“OK. I’ll try somebody else!”)
input(“\n\nPress enter to exit.”)
As soon as you enter “No.” it will break out of the loop, because you’ve broken the condition the loop was set to. It was saying when your response was not equal to “No.”, it will keep repeating itself in a loop.
The reponse = “” is called a sentry variable. It is a barrier around the block of the while loop, and of course, every time we input something, we’re updating the response variable. You should initialise a sentry variable before your loop. If you put it in, you’ll have an infinite loop. Welcome to the club.
# Quick way to join the club
penguin = 0
while penguin <= 0:
Values — True and False
I’ve mentioned these 2 values a lot. In Python any value is true or false. Integers, decimal numbers, and strings are true. 0 and “” (empty string) are false. Programmers test for empty values, perhaps to make sure the user input a number, etc.
print(“Would you like to make a donation to the programmer’s trust fund?”)
donation = int(input(“How much would you like to donate to the cause?”))
print(“Thanks very much!”)
print(“You chose not to donate anything to the cause.”)
- Notice “if donation:” — it means “if there is a donation” as well as meaning if donation != 0.
- It’s a clear, elegant way of saying it. It relates back to its true and false values.
Break and Continue statement
var = 0
count += 1
if count > 10:
if count == 7:
- As soon as the count variable reaches 10, it breaks out of the loop.
- When count hits 7, it skips the rest of the loop, skips 7, and goes back through the loop.
- Try running this, and with your own numbers.
Logical Operators: and, or, not
# Favourite colour and favourite day clubs
print(“\tLet’s see what groups you fall in to”)
colour = “”
while not colour:
colour = input(“Favourite colour?: “)
day = “”
while not day:
day = input(“Favourite day?: “)
if colour == “red” and day == “monday”:
print(“You’re in the ‘Red Mondays’ group!”)
elif colour == “blue” and day == “tuesday”:
print(“You’re part of the ‘Blue Tuesdays’ group!”)
# and so on and so forth
print(“I was too lazy to come up with more options..”)
input(“\n\n\tHit enter to quit”)
- The user has to enter some of the (limited) options before I let them continue through the loop.
- Using the logical not operator spawns a phrase to mean the opposite of the original.
- username starts out as false – an empty string
- Soon as the user enters something, the value is true, and then proceeds through the loop
username : not username
True : False
False : True
and is straight forward. Put it between 2 conditions when you want a new condition that is true if both of them are true.
You walk into a shop. You want to buy oranges and apples. You’ll only buy if they have both of them.
or is also simple. 1 of the conditions has to be true.
You want to buy oranges or apples. If they had only one fruit, you’d still buy.