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
Jesse Davidson
647 PointsLoop issue
Good day
I'm struggling to understand one thing in looping (
See below code:
number_of_attempts = 1
password = input("Password - ") while password != "Wonderbar": if number_of_attempts >= 5: sys.exit("Invalid number of attempts, plesae try again later") password = input("Incorrect password, try again - ") number_of_attempts = number_of_attempts + 1
Can you please explain how the variable (number_of_attempts) is able to obtain a new value based on if the password is incorrect and adds a value to state it must add 1.
The input value of (password) is a string and number_of_attempts is separate variable on it's own that does not call any other values.
I'd understand if the value was was passing through as the variable obtains a new value based on the in example below:
count = 0 while count < 100: print(count) count = count +1
Would really appreciate your feedback.
3 Answers
Marco Cornejo
3,412 Pointsnumber_of_attempts = 1
password = input("Password - ")
while password != "Wonderbar": #While the password entered by the user is NOT 'Wonderbar', do the following:
if number_of_attempts >= 5: #user has 5 attempts to get his password right
# If the user has already attempted his password 5 times, don't let him.
sys.exit("Invalid number of attempts, please try again later")
#Let the player know his password is incorrect, and let him input again
password = input("Incorrect password, try again - ")
number_of_attempts = number_of_attempts + 1
# add 1 to the number of attempts, AND GO BACK TO CHECK THE WHILE LOOP, to see if the password is 'Wonderbar'
#Once the password is exactly 'Wonderbar', the user may proceed.
mhjp
20,372 Points# set number_of_attempts to 1
number_of_attempts = 1
# ask user to input password
password = input("Password - ")
# loop while password is not equal to Wonderbar
while password != "Wonderbar":
# check if number_of_attempts is greater than or equal to 5
if number_of_attempts >= 5:
# exit with message if above statement is True
sys.exit("Invalid number of attempts, plesae try again later")
# ask for password again
password = input("Incorrect password, try again - ")
# increment number of attempts by 1
number_of_attempts = number_of_attempts + 1
The comments should clarify things a little. Also remember that number of attempts is a global variable, it is instantiated outside the loop so is available in the scope of the while loop.
Once the loop runs the first time we go right back to "while password != "Wonderbar" and run the block again. on the second run number_of_attempts = 2, and we repeat until number_of_attempts = 5 then we exit. If the password is correct we exit the loop and the program because there is noting else to run.
Jesse Davidson
647 PointsThanks guys.