Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping While Loops

Abdullah Jassim
Abdullah Jassim
4,551 Points

After the first attempt, I dont see the message - you have made 1 attempt. I only see if after second attempt. Guidance?

import sys

master_password = "opensesame"
password = input("Enter the password ")
attempt = 0


while password != master_password:
    if attempt < 3:
        password = input("Invalid password. Try again. ")
        attempt += 1
        print("You have made {} out of 3 attempts".format(attempt))
    else:
        sys.exit("Too many attempts")

print("Welcome to secret world")

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,361 Points

Hi Abdullah,

I reordered your version and would suggest this approach.

import sys

master_password = "opensesame"
password = input("Enter the password ")
attempt = 0


while password != master_password:
    if attempt < 3: # you should increase the attempt count before the print statement
        attempt += 1
        print("You have made {} out of 3 attempts".format(attempt))  # print the updated count
        password = input("Invalid password. Try again. ")
    else:
        sys.exit("Too many attempts") # if the user tries to log in more than three times the program is stoped

print("Welcome to secret world")

What do you think?