Week 3: If Statements¶
Syntax¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var1 = True
var2 = False
if var1:
print("var1 is True!")
if not var2:
print("'not var2' is True!")
if var2:
print("This will never print")
elif not var1:
print("This won't print either!")
else:
print("This is the final catch-all condition!")
|
Exercises¶
Inputs and Ifs¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | print("Welcome to my practice login screen")
username = input("Username> ")
password = input("Password> ")
##### Fill in the if, elif, and else to test for two usernames.
username0 = "euclid"
password0 = "bunny"
if username0 == "euclid":
if password0 == "bunny"
print("You logged in!")
else:
print("Wrong password!")
elif #### username 1 ####
if #### password 1 ####
else
elif #### username 2 #####
if #####password 2 #####
else
else
print("I don't know that username!")
|
Bonus¶
Change the above code so that instead of having nested if
statements,
it uses a boolean and
to combine the username and password tests together.