Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,365 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?