A5 - Extending the if Statement
In this assignment, we'll look at how we can do more with if statements.
The and and or Boolean operators
We can use and and or to create more complex Boolean conditions for our if statements.
Suppose we wanted to see if a variable contains a number between 10 and 20. Here's how we do it:
num = int(input("Enter a number: "))
if num >= 10 and num <= 20:
print("The number is between 10 and 20")
Suppose we wanted to check if a variable contains any one of a certain set of strings. Here's how we do it:
color = input("Enter a colour: ")
if color == "red" or color == "blue" or color == "gold":
print("You chose an Eastview colour")
Warning
On each side of an and or or operator, there needs to be a full Boolean expression.
A common mistake would be to try to write these examples like this:
if num >= 10 and <= 20:
print("The number is between 10 and 20")
if color == "red" or "blue" or "gold":
print("You chose and Eastview colour")
Although the above code reads intuitively, Python will not understand it.
Using else and elif
Often, we'll want to do one thing when the if condition evaluates to True, and something different if it evaluates to False.
Here's a quick way to do this:
guess = input("Guess the password")
if guess == "123456":
print("You guessed my very insecure password!")
else:
print("Nope, that's wrong!")
Finally, let's suppose we want to ask the user a question, and we want to respond differently depending on whether they answered "yes", "no", "maybe", or something else. Here, we can use elif, which is short for "else if".
reply = input("Will you have homework today?")
if reply == "yes":
print("Too bad!")
elif reply == "no":
print("Enjoy some free time!")
elif reply == "maybe":
print("Hopefully not!")
else:
print("I didn't understand your answer.")
print statements will run.
It will check for a match in order, and run the code under the first match it finds.
The code under the else only runs if no matches were found.
Your Task
Create a file called a5.py.
For the first part of this assignment, write a program similar to the last example above that asks the user a question with multiple valid answers.
Your question can be of the yes/no variety shown above or something different, as long as it has at least two valid answers.
After asking the question and getting a reply, you will need to create an if statement that has at least one elif clause, as well as an else clause at the end in case they entered an invalid answer.
Use the example above to guide you.
Below that, ask the user their age. You should then tell them their cost of admission into a movie based on the following criteria:
- If they're under five, they're free
- If they're between 5 and 12 (inclusive), they pay $10
- If they're between 13 and 17 (inclusive), they pay $12
- If they're between 18 and 64 (inclusive), they pay $15
- if they're 65 or over, they pay $13
You'll need to structure this in the same way as the first part, but you'll find you'll
need to make some use of the and operator for some of the conditions.