Here's a syntax example for reference.
In [45]:
num = int(input("Number? "))
if (num < 40):
print("Too low")
elif (num > 50):
print("Too high")
elif (num == 42):
print("Just right!")
else:
print("You're sort of close.")
Write code that represents this situation.
Then it will print a message.
Make sure you run your code several times and test all 3 scenarios!
In [ ]:
Here is the day/season example from the lesson. Modify it to include autumn day and night.
In [ ]:
time = input("What time of day is it? Answer day or night: ")
season = input("What season is it? Answer winter, summer, or spring: ")
print("")
if (season == "winter"):
if(time == "day"):
print("Winter in the daytime can be very bright with snow.")
else:
print("Winter at night is beautiful with moonlight on the snow.")
elif (season == "summer"):
if(time == "day"):
print("Summer days are for play!")
else:
print("Summer nights are warm but not bright.")
else:
if(time == "day"):
print("Spring flowers are so colorful in the daytime rain.")
else:
print("Spring nights are chilly.")
print("See you later!")
Bonus: Combining boolean conditions
You can use and, or, not, and parentheses to combine boolean variables logically. Here are several examples and then a couple more challenge problems.
In [53]:
a = False
b = False
c = True
In [48]:
print(a and b)
In [49]:
print(a and c)
In [50]:
print(a or c)
In [52]:
print(not False)
In [54]:
print(not a)
In [56]:
print(True and True)
As you might have figured out, ThingA "and" ThingB is only true when both ThingA is true and ThingB is true. Likewise, "ThingA and ThingB and ThingC and ThingD" is only true when all of them are true.
ThingA "or" ThingB is true when any one of them is true. Likewise, "ThingA or ThingB or ThingC or ThingD" is true if any one of them is true.
"Not" flips the meaning. That which is not true, is obviously false. That which is not false, is true.
What does this code print?
Think about each one, decide on your answer, and then run it to check your results.
In [ ]:
print(True and True and True and False and True)
In [ ]:
print(True or True or True or False or True)
In [ ]:
print(False or False or False or True or False)
In [ ]:
print(False and False and False and True and False)
Here's an example of representing a condition using a combination of boolean variables.
In [59]:
hasUnreadEmail = True
hasTimeToReadEmail = True
if (hasUnreadEmail and hasTimeToReadEmail):
print("Reading email now.")
hasTimeToReadEmail = False
if (hasUnreadEmail and hasTimeToReadEmail):
print("Reading email again.")
In [60]:
x = 20
y = 10
z = 30
if ((x > y) and not (z <= y)):
print("Special case")
print("Bye")
Here are some boolean (true-false) variables that represent certain conditions.
In [57]:
isDaytime = False
isRaining = True
isWarmOutside = True
Using those variable names, write a boolean expression that represents the following statement (goForWalk should be true only when all of the appropriate conditions are met).
I will go for a walk only during the day, when it is warm outside, and not raining.
In [ ]:
goForWalk = ?
The final box is for you to experiment with your own ideas. Get creative! You know enough code now to create some interesting things!
In [ ]: