ุงู†ุชู‚ู„ ู„ู„ู…ุญุชูˆู‰
Robo EgyptRobo Egypt
โ˜ฐ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

๐Ÿ› ๏ธ 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:
Password: robo123
Welcome! ๐Ÿ”“
๐Ÿ Open the code in Robo Python โ†—

๐Ÿ“Œ 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.

๐Ÿ A password and a user name
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:
User: robo
Password: 1234
Welcome!
๐Ÿ Open the code in Robo Python โ†—
โœ๏ธ Practise
Challenge: ask for the user name too, and open only if both are right (and).
โœ๏ธ Practise here in the lessonor open the lab in a new page โ†—

๐ŸŽฏ Challenge yourself

Easy

The password โ€œsunโ€: print open or closed.

๐Ÿ‘€ Show the solution

When the program asks, we type: moon.

๐Ÿ Solution
pw = input("Password: ")
if pw == "sun":
    print("open")
else:
    print("closed")
๐Ÿ–ฅ๏ธ The output in Robo Python:
Password: moon
closed
๐Ÿ Open the code in Robo Python โ†—

Result: closed

Medium

The password is not accepted if it is shorter than 6 letters (len).

๐Ÿ‘€ Show the solution

When the program asks, we type: abc.

๐Ÿ Solution
pw = input("New password: ")
if len(pw) < 6:
    print("too short")
else:
    print("saved")
๐Ÿ–ฅ๏ธ The output in Robo Python:
New password: abc
too short
๐Ÿ Open the code in Robo Python โ†—

Result: too short

Hard

Only three tries for the password (for).

๐Ÿ‘€ Show the solution

When the program asks, we type: 1111, then 1234.

๐Ÿ Solution
for i in range(3):
    pw = input("Password: ")
    if pw == "1234":
        print("Welcome!")
        break
    print("wrong")
else:
    print("locked")
๐Ÿ–ฅ๏ธ The output in Robo Python:
Password: 1111
wrong
Password: 1234
Welcome!
๐Ÿ Open the code in Robo Python โ†—

Result: wrong once, then Welcome!

Finished the lesson? Mark it so your progress counts.