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

Angel Naranjo
Angel Naranjo
30 Points

I don't get why you put attempt_count += 1 inside the if block. Isn't this equal to 2?

import sys

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")

Hello Angel,

It is difficult to read your code because of the formatting. Please read the Markdown Cheatsheet to learn how to show your code in forum posts. It is below the "Add an Answer" area :arrow_down:

2 Answers

Steven Parker
Steven Parker
231,269 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.

Angel Naranjo
Angel Naranjo
30 Points

Why is it that I get 4 attempts when I write the code this way:

import sys

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")

It will let you get up to 4 wrong answers, but if I write like this:

import sys

password = input("Please enter the super secret password: ")
attempt_count = 1

while password != "opensesame":
    attempt_count += 1
    if attempt_count > 3:
        sys.exit("too many invalid password attempts")
    password = input("Invalid password, try again: ")

It gives me 3 attempts.

I reversed the attempt_count += 1

Steven Parker
Steven Parker
231,269 Points

The difference is the first example tests to see if the value is more than 3 before it changes it; But in the second example it changes it first, and then tests to see if it is more than 3.

So because of the order, the first example repeats one more time than the second one.

Angel Naranjo
Angel Naranjo
30 Points

If this code were to go first:

    if attempt_count > 3:

The value for attempt_count can instantly reach 4 attempts by the user and be greater than 3.

However, if this code were to go first:

    attempt_count += 1

The value for attempt_count would be the number of attempts by the user, which would max out at 3 because adding 1 will make it 4. Then it follows with the if attempt_count > 3: code.

Thanks, Steven

Steven Parker
Steven Parker
231,269 Points

I'm not sure what you mean by "instantly". The loop still must repeat several times before the value is larger than 3.

Angel Naranjo
Angel Naranjo
30 Points

Yes, you are correct. "Instantly" was not the proper wording. The code will repeat until the number of attempts is greater than 3.

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.

Angel Naranjo
Angel Naranjo
30 Points

Why is it that I get 4 attempts when I write the code this way:

import sys

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")

It will let you get up to 4 wrong answers, but if I write like this:

import sys

password = input("Please enter the super secret password: ")
attempt_count = 1

while password != "opensesame":
    attempt_count += 1
    if attempt_count > 3:
        sys.exit("too many invalid password attempts")
    password = input("Invalid password, try again: ")

It gives me 3 attempts.

I reversed the attempt_count += 1

Hey Angel,

Yep it gives 4 attempts because we used

if attempt_count > 3

if you wanted to have it only allow 3 attempts, use >= 3 or you could use >2.