๐ ๏ธ Section project: the password guard
Section 3: ConditionsLesson 3.4 ยท โฑ 10 min
๐ฏ Project goal
The program asks for the password. If it is right it says Welcome, otherwise it says Wrong password.
password = input("Password: ")
if password == "robo123":
print("Welcome! ๐")
else:
print("Wrong password ๐")
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โPassword: robo123 Welcome! ๐
๐ Another example: a password and a user name
You get in only if the name and the password are both right.
When the program asks, we type: robo, then 1234.
user = input("User: ")
pw = input("Password: ")
if user == "robo" and pw == "1234":
print("Welcome!")
else:
print("Wrong user or password")
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โUser: robo Password: 1234 Welcome!
โ๏ธ Practise
Challenge: ask for the user name too, and open only if both are right (and).
โ๏ธ Practise here in the lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
The password โsunโ: print open or closed.
๐ Show the solution๐ Hide the solution
When the program asks, we type: moon.
pw = input("Password: ")
if pw == "sun":
print("open")
else:
print("closed")
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โPassword: moon closed
Result: closed
Medium
The password is not accepted if it is shorter than 6 letters (len).
๐ Show the solution๐ Hide the solution
When the program asks, we type: abc.
pw = input("New password: ")
if len(pw) < 6:
print("too short")
else:
print("saved")
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โNew password: abc too short
Result: too short
Hard
Only three tries for the password (for).
๐ Show the solution๐ Hide the solution
When the program asks, we type: 1111, then 1234.
for i in range(3):
pw = input("Password: ")
if pw == "1234":
print("Welcome!")
break
print("wrong")
else:
print("locked")
๐ฅ๏ธ The output in Robo Python:
๐ Open the code in Robo Python โPassword: 1111 wrong Password: 1234 Welcome!
Result: wrong once, then Welcome!

