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.
Angel Naranjo30 Points
I don't get why you put attempt_count += 1 inside the if block. Isn't this equal to 2?
password = input("Please enter the super secret password: ") attempt_count = 1 while password != "opensesame": if attempt_count > 3: sys.exit("Too many invalid password attempts") password = input("Invalid password, try again: ") attempt_count += 1
print("Welcome to secret town")
Steven Parker210,474 Points
The statement "
attempt_count += 1" will increase the value in "attempt_count" by one. So if it begins as 1, the first time this statement is encountered, it will change to 2. But the next time it will become 3, and then 4, as the loop continues to repeat.
attempt_count += 1 would execute after an attempt, at some point prior to this section of code you would have attempt_count = 0 most likely. So attempt_count += 1 would set it to 1 the first time it is triggered, 2 the second time and so on.