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 trialsulaiman abouabdah
5,314 Points4 attempts in the video instead of 3
import sys
MASTER_PASSWORD = 'opensesame'
password = input("Please enter your password here: ")
attempt_count = 1
while password != MASTER_PASSWORD :
if attempt_count >=3:
sys.exit('Too many invalid password attempts')
password = input('invalid password, try again: ')
attempt_count += 1
print('Welcome Dude!')
I think if attempt_count>3 is incorrect. you should change it to attemp_count>=3 like the code above because the first time the user enters their password, it should be counted as one try
Here is the output of the code mentioned in the video:
C:\Desktop\TeamTreehouse\Python Basics>python password_checker.py
Please enter your password here: lumberjack # Try number 1
invalid password, try again: cheeseshop # Try number 2
invalid password, try again: dudydude # Try number 3
invalid password, try again: yo yo yo # Try number 4 <--- This shouldn't happen
Too many invalid password attempts
the output of the corrected code above
C:\Desktop\TeamTreehouse\Python Basics>python password_checker.py
Please enter your password here: lumberjack
invalid password, try again: cheeseshop
invalid password, try again: Superman
Too many invalid password attempts
MOD: I formatted the output too, to make it easier to read. Thanks!
3 Answers
Konrad Gołda
735 PointsAnd is it OK when I replace this whole line 'attempt_count += 1' in between 'while' line and 'if' line? Works for me, but I guess Craig had some reason to put this line at the end. Have a look below. Thanks!
while password != MASTER_PASSWORD:
attempts += 1
if attempts > 3:
sys.exit("Too many invalid attempts :(")
password = input("Invalid pass, try once more: ")
Katie Ferencik
1,775 Pointssulaiman abouabdah I realize this is a really old post, but it doesn't look like anyone answered it. I thought the same thing, so I changed my if attempt_count to == 3
rather than> 3
(which seems to make more sense if you're trying to limit it to 3...I mean, you're never going to go above that once you reach it anyway, right?)
MASTER_PASSWORD = "opensesame"
password = input("Please enter the super secret password: ")
attempt_count = 1
while password != MASTER_PASSWORD:
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")
Katie Ferencik
1,775 PointsKonrad Gołda - same thing with you re: old post but I thought I'd answer anyway.
I believe it doesn't matter if your attempt_count += 1
is where you placed it vs. where Craig places it in the tutorial, since it's basically doing the same thing either way. I agree with how you put it first though, it makes a lot of sense to put it first just because it's naturally the way we would think about it.
vladimirv
12,124 Pointsvladimirv
12,124 PointsI did the same, it worked.