'how would I reset the counter
How could I make count zero after the time ends so that It could restart and ask for the user to input the password and username again? I've tried to add count = 0 at the end of the while loop and that seemed to not work ( as i thought ) then i tried to do IF t == 0: count = 0 after the time while loop and it didn't work. python stated that the local variable was referenced before assignment. I've changed somethings to my previous code however I'm still a bit confused on how to reset the counter. if someone could help that would be great!
username = input('what would you like your username to be? ')
password = input('What would you like your password to be? ')
#make sure the users password is secure
while len(password)<8:
print("please create a stronger password it must contain 8 charaters")
password = input('What would you like your password to be? ')
else:
print (" Your password is secure!")
#inputed username become new username
#inputed password becomes new password
new_username = username
new_password = password
#ask user to enter username and password
print('Enter your username and password to continue')
#the user will only have 3 chances to enter their password
count=0
while count < 3:
#the user is asked to input their username and password
username = input('Enter username: ')
password = input('Enter password: ')
if password == new_password and username == new_username:
print('Access granted')
count += 0
break
else:
print('Access denied. Try again.')
count += 1
# import the time feature into program
import time
# define the countdown functio
def countdown(t):
if count == 3:
print ('you can try again in :')
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
else:
print ("welcome",new_username,"here are your files")
# the time in seconds
t = 30
countdown(int(t))
Solution 1:[1]
try this it will work fine with you you need to change the if on count to be in the access denied else section there is the code I try
# import the time feature into program
import time
username = input('what would you like your username to be? ')
password = input('What would you like your password to be? ')
# make sure the user's password is secure
while len(password) < 8:
print("please create a stronger password it must contain 8 charaters")
password = input('What would you like your password to be? ')
else:
print(" Your password is secure!")
# inputted username become new username
# inputted password becomes new password
new_username = username
new_password = password
# ask user to enter username and password
print('Enter your username and password to continue')
# the user will only have 3 chances to enter their password
count = 0
while count < 3:
# the user is asked to input their username and password
username = input('Enter username: ')
password = input('Enter password: ')
if password == new_password and username == new_username:
print('Access granted')
exit()
else:
print('Access denied. Try again.')
count += 1
if count == 3:
print('you can try again in : 30 sec')
exit()
# define the countdown function
def countdown(t):
while t:
mints, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mints, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
# the time in seconds
timer_count = 30
countdown(int(timer_count))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
