Unit 3 · Decisions, Decisions · Lesson 29 of 57
An donut?
Oh... And, or, not!
Sometimes one question isn't enough. A login needs the right username and the right password, not just one of them.
Python lets you combine questions with three words:
and:Trueonly if both sides areTrueor:Trueif at least one side isTruenot: flips it.not TrueisFalse, andnot FalseisTrue
username = input("Username: ")
password = input("Password: ")
if username == "karl" and password == "purple4life":
print("Welcome back, Karl!")
else:
print("Nice try.")
If the username is right but the password is wrong, and says False. Both have to be right.
if username == "alex" or username == "amer":
print("Hmm, we're keeping an eye on you.")
With or, either one is enough.
is_admin = False
if not is_admin:
print("Admins only!")
not is handy when you want to check that something isn't the case.
Let's write a slightly smarter password checker.
Make a function called is_strong that takes a password and returns True only if:
- it's at least 10 characters long, and
- it's not
amer1234567(Amer tried. We saw.)
Otherwise, it should return False.
You don't need to print anything, just return. (Remember, password != "something" is already True or False!)
Python exercise
Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.