Join 36000+ teachers and students using TTIO.
So far, we haven't been able to do anything conditionally; we cannot tell Python "do this in some cases and do this other thing" in others. This is the sort of logic we really need and indeed we use it all the time in making every day decisions!
Programming languages like Python have tools to mimic those real-world choices.
Many of the decisions we make throughout the day – "it's raining - get an umbrella"; "I have a cough - uh oh, maybe I need to call in sick"; "If my tummy rumbles, I'll head to the fridge" – have an if-then structure. Just like you, Python can make if-then decisions.
If statements are how you direct Python to do something if something else is true. For example – run the code below. What do you think it'll do?
If the user enters the right secret code (007) then access is granted, else denied.
#Cut and paste the code below into this trinket
code=input("Enter secret code:")
if code=="007":
print("Access Granted")
else:
print("Impostor. Sorry, goodbye!")
www.teachyourselfpython.com