Python username and password with 3 attempts
Solution 1
Fixed the code to achieve what you are trying to do:
print('Enter correct username and password combo to continue')
count=0
while count < 3:
username = input('Enter username: ')
password = input('Enter password: ')
if password=='Hytu76E' and username=='bank_admin':
print('Access granted')
break
else:
print('Access denied. Try again.')
count += 1
Changes that have been made:
- Removed the definition of
username
andpassword
since it is redundant and can be omitted - Changed the
while
statement to count 3 iterations ofcount
- Validation of the credentials only in the
if
statement and not in thewhile
- Changed the decreasing of
count
to increasing (fromcount -=
tocount +=
) break
the loop when the right credentials are entered
Solution 2
here try this (I try to change your code as less as possible so that you can identify the same logic yourself)
print('Enter correct username and password combo to continue')
count = 0
# "" or '' because you are assigning a value string into it
password = ""
username = ""
# looping will continue when wrong input for three times and ask again...
while password!='Hytu76E' and username!='bank_admin' and count < 3:
# you are collecting user input from CLI separately (you can not assign and operator to such operation as per your code ;)
username = input("Enter username: ")
password = input("Enter password: ")
if password=='Hytu76E' and username=='bank_admin':
# if match, grand and break
print('Access granted')
break
else:
print('Access denied. Try again.')
count+=1 # as per gbse, in the comments, you will need the + to count up
issues in your code:
# you are assigning string value, what for? this would make the loop hit positive the first time
password=Hytu76E # string assignment error in syntax, anyway
username=bank_admin # string assignment error in syntax, anyway
# you can not assigning and operator in the input because of no if condition in this line, also you should compare the values of the input
username=input('Enter username: ') and password=input('Enter password: ')
# if code is ok, then move outside the loop in the case when the user enters the first time good answers
if password=='Hytu76E' and username=='bank_admin':
print('Access granted')
else:
print('Access denied. Try again.')
# you are decremented the counter which would never leave teh loop at 4, you should add one on each iteration so count+=1 (count = count + 1)
count-=1
Roland Weisleder
I'm a professional Java software engineer with more than 10 years of experience in software development. I also have several years of experience in the analysis and modernisation of legacy software. My technical focus is on Java, Spring, Java EE, Camunda, and Apache Maven. I work as freelancer. So if you need help with your projects: feel free to contact me directly. I also contribute to the open-source projects I use.
Updated on January 20, 2022Comments
-
Roland Weisleder over 2 years
Just started python and racking my brains on this but can't seem to get it right.
print('Enter correct username and password combo to continue') count=0 password=Hytu76E username=bank_admin while password!='Hytu76E' and username!='bank_admin' and count<4: username=input('Enter username: ') and password=input('Enter password: ') if password=='Hytu76E' and username=='bank_admin': print('Access granted') else: print('Access denied. Try again.') count-=1
syntax error, can't assign to operator on line 6 username=input.
-
Van Peer over 6 yearsformat your code. you've while condition which is negating the if condition inside it.
-
depperm over 6 yearschange
count-=1
tocount+=1
and then remove the redundant username/password check in the while loop -
Vasilis G. over 6 yearsShouldn't the values
password
andusername
be inside quotes, in the beginning?
-
-
Jerrybibo over 6 yearsThis does not properly re-evaluate the inputs repeatedly.
-
PM 2Ring over 6 yearsThat's better, but you still have those redundant assignments. They don't do anything useful, they just waste space & time.