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
Elgene Ee
3,554 PointsCan't execute password
Hi, I am currently building a simple program that allow the user to enter the password, if the password is correct then it enters, but if it fails, the user has four tries left (5 tries total)...but what is wrong with my code? Should i use for loop or while on this one?
password = ('1234')
myVar = input("Enter password")
count = 5
for items in myVar:
if password == myVar:
print('Good Job!')
break
else:
print('Try again')
count -= 5
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,888 PointsHI Elgene,
Let's step through your code and look at what each part of it does.
password = ('1234')
First, enclosing your string in parentheses doesn't actually break any code, but it is redundant.
myVar = input("Enter password")
...
for items in myVar:
Here you've created a variable called myVar that holds a string. Then you use a for loop to iterate through each character in the string, assigning at each iteration the character to the variable items.
Remember that for loops always iterate through some definite sequence, whether that is a dictionary or an array, or a string, or whatever. If you want the loop to repeat some definite number of times that you know at the time you enter the loop, then you want a for loop. If you want the loop to repeat some indefinite number of times, you want a while loop. In your case, you do want to run your loop a specific number of times: 5, not the number of times that is equal to the number of characters in the user's text entry. So your for loop should be based on count instead of myVar.
One way that you could achieve this would be to say something like:
for count_index in range(count):
(assuming you want access to the count iteration during the loop). If you never need to see how many tries the user has made, you can tell Python not to bother assigning it to a variable as follows:
for _ in range(count):
Inside your loop, your if branch of the conditional is fine, but the else branch doesn't make sense.
count = 5
for items in myVar:
...
else:
...
count -= 5
You never have any logic in your code that tests anything against count, so reducing the count number by 5 every time you go through the loop isn't going to accomplish anything. Even if you did want to test against the count, chances are that you would want to change the value of count by 1 each time, not 5.
In the code I suggested above, you'd never need to test against count (the loop will end automatically once the range has been iterated through), but you would have the opportunity in your else branch to tell the user something like:
...
print("You have {} tries remaining".format(count - count_index - 1))
...
Hope the above clears things up for you.
Cheers
Alex