Branching AND OR NOT
and.py
roomClean = False
homeworkDone = True
if roomClean and homeworkDone:
print("You can go out to play.")
else:
print("You haven't finished your chores.")
or.py
position = 1 #1st place!
time = 10.22
personalBest = 9.87
if position == 1 or time < personalBest:
print("Great run!")
else:
print("Never mind")
not.py
feeling = "happy"
if not(feeling == "happy"):
print("Cheer up brussel sprout!")
else:
print("That's great, keep smiling!")
complex logic.py
foul = False
speed = 220.4 #220.4 km/hr
outcome = "returned"
if not(foul) and (speed >= 200.0 or outcome == "Ace"):
print("Great serve!")
- to craft a stone pickaxe in Minecraft i need 3 cobblestones and 2 sticks. Create two variables to store the number of cobblestones and sticks i have. If i have enough of both, say "pickaxe crafted". If i dont, say "not enough resources". You may need a more complex boolean expression to check both variables at once, e.g.:
if cobblestones >= 3 and sticks >= 2: - to craft an axe (aka hatchet) in Minecraft i can use 2 sticks plus either 3 cobblestones or 3 wood planks. complete the same task as the previous question, however this time say "axe crafted" instead of "pickaxe crafted".
- create three variables
raincoats,umbrellasandpuddles. Set each of these variables to a random number between 1 and 6. If i have moreraincoatsandumbrellascombined than puddles, print "staying dry".