Reputation: 19
Hello i am trying to create a small code that receives password attempts for a number of times (3) , and if the password is wrong , the user can't try again .
can you please tell me why my code is not giving me the expected results i want?
the results it is giving me is a loop that never ends to ask for a try again ; while te results i desire is a loop that ends after 3 trials only . "i would love to keep in the while loop"
thank you very much
my code is
password=''
for i in range(1,3):
if i<3:
while password!='daniel':
password=input("enter the password:")
if password=="daniel":
print('your logged in ')
else:
print('try again ')
else:
print("number of trials done")
`
Upvotes: 1
Views: 406
Reputation: 1700
for i in range(0,3):
if i<3:
password = input("enter the password:\n")
if password=="daniel":
print('your logged in ')
break
else:
print('try again ')
continue
else:
print("number of trials done")
Or
password=''
i = flag = 0
while password!='daniel'and i<3:
password=input("enter the password:\n")
if password=="daniel":
print('your logged in ')
flag=1
else:
print('try again ')
i +=1
if flag==0:
print("number of trials done")
Upvotes: 0
Reputation: 538
The first problem with you code is you are using password
with out intializing it. second the while loop will run forever unless the correct password is given. third even with the correct password given the while loop has a chance to restart for another 2 rounds
Here is an alternative
for trial in range(3):
password=input("enter the password:")
if password=="daniel":
print('your logged in ')
break
elif trial == 2:
print("number of trials done")
else:
print('try again ')
trial = trial + 1
Upvotes: 0
Reputation: 274
Using both for loop and nested while loop makes the number of trials has no end. In addition, you should assign the variable password
before you add it to the condition, or you can use while True
and break
instead.
counter = 0
while True:
if counter < 3:
counter += 1
password = input('Enter the password: ')
if password == 'daniel':
print("You're logged in")
break
else:
print('Try again')
else:
print('Number of trials is done. Please try again later.')
break
Or you can do it using for loop.
for i in range(0,4):
if i < 3:
password = input('Enter the password: ')
if password == 'daniel':
print("You're logged in")
break
else:
print('Try again')
else:
print('Number of trials is done. Please try again later.')
break
Upvotes: 0
Reputation: 17156
for i in range(3):
password=input("enter the password:")
if password=="daniel":
print('your logged in ')
break
elif i < 2:
print('try again ')
else:
print('Number trials exceeded')
Upvotes: 1
Reputation: 97
When you run this code you should get the error: NameError: name 'password' is not defined
. This is because Python runs through your code line by line, and when it gets to the line while password!='daniel'
it has no idea what password
is because it hasn't yet been told, and so just stops running.
To fix errors like this you just need to first tell Python that password
is going to be a variable which will store text. You can initialise the variable with empty text like so: password=''
, this must be put anywhere before the line that causes the error.
Upvotes: 1