Sometimes an if statement does not allow for enough possible outcomes. Look at the example below.
<aside> 💡 Aisha wants to input today’s weather forecast into a program she is writing. She wants the program to give a comment based on the temperature.
if the temperature is less than 0, the program should output → “it may be safer to stay in” if the temperature is between 0 and 5 → “It will be freezing today” if the temperature is between 6 and 10 → “It will be quite chilly today” if between 11 and 18 → “it will be a mild day” if between 18 and 25 → “The temperature will be very hot today, remember sunscreen!” and for all other temperatures output → “It will be scorchio!
</aside>
You’ll be writing the program for the above scenario in just a moment. Take a look at the example syntax below for a similar solution.
goals = int(input("How many did arsenal score last night?"))
if goals == 0:
print("Arsenlol always make me laugh 😂")
elif goals >=1 or goals <=2:
print("its nice to see they score a few")
elif goals >2 or goals <5:
print("It sounds like it may have been an exciting game")
else:
print("Wow! Arsenal must have thrashed the other team")
Key points from the above example:
if
elif
else
statement but is not necessary.True
or False
comparison that is combined with an or
operator.
When this is the case, you must refer to the object being compared twice. eg.
✔ goals >0 or goals <=2
❌ goals >0 or <=2
A popular game we’ve all probably played to help make a decision such as what you’re watching on TV has been Rock-Paper-Scissors. In this example, you will see 2 things that are new to you.
from random import choice
allows Python to load a function to make random choices from a library of functions called random
. You will learn more about the random library later.if
Boolean expression uses an and
operator checking to see if 2 conditions are True
. You will finish this code off for task 2!https://replit.com/@bitFez/eliftask2-rock-paper-scissors#main.py
https://youtube.com/shorts/jgFurizupMI?si=VROYJxTgfvVsJBfu