Home > front end >  how can I reset the counter?
how can I reset the counter?

Time:02-02

I want the counter to reset to zero after the 30-second timer ends so if the user gets it wrong 3 times they can retry after the time is up. I'm using python btw

here is the section I need help with:

count=0
while count < 3:
  #the user is asked to input their username and password
    username = input(Fore.WHITE   'Enter username: ')
    password = input(Fore.WHITE   'Enter password: ')
    if password == new_password and username == new_username:
        print(Fore.GREEN   'Access granted')
        count  = 0
        break
    else:
        print(Fore.RED   'Access denied. Try again.')
        count  = 1
        
# import the time feature into the program
import time
  
# define the countdown func.
def countdown(t):
 if count == 3:   
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")
        time.sleep(1)
        t -= 1
    print ('you can try again in :') 
 else:
    print ("welcome",new_username,"here are your files")
   
# the time in seconds 
t = 30

countdown(int(t))

CodePudding user response:

For the password, use the getpass it is better than input.

Solution 1:

import time
from colorama import Fore

new_username = 'stackoverflow'
new_password = 'stackoverflow'

def login(_t):
    for _ in range(3):
        # the user is asked to input their username and password
        username = input(Fore.WHITE   'Enter username: ')
        password = input(Fore.WHITE   'Enter password: ')
        if password == new_password and username == new_username:
            print(Fore.GREEN   'Access granted.')
            print(f"welcome {new_username} here are your files")
            return True
        else:
            print(Fore.RED   'Access denied. Try again.')

    for i in range(_t, 0, -1):
        mins, secs = divmod(i, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(f'\ryou can try again in : {timer}', end='')
        time.sleep(1)
    print('\r', end='')
    login(_t)

t = 3
login(t)    

Solution 2:

import time
from colorama import Fore

new_username = 'stackoverflow'
new_password = 'stackoverflow'

def login():
    for _ in range(3):
        # the user is asked to input their username and password
        username = input(Fore.WHITE   'Enter username: ')
        password = input(Fore.WHITE   'Enter password: ')
        if password == new_password and username == new_username:
            print(Fore.GREEN   'Access granted.')
            return True
        else:
            print(Fore.RED   'Access denied. Try again.')


# define the countdown func.
def countdown(_t):
    if login() is True:
        print(f"welcome {new_username} here are your files")
    else:
        for i in range(_t, 0, -1):
            mins, secs = divmod(i, 60)
            timer = '{:02d}:{:02d}'.format(mins, secs)
            print(f'\ryou can try again in : {timer}', end='')
            time.sleep(1)
        print('\r', end='')
        countdown(_t)

# the time in seconds
t = 3
countdown(int(t))
  •  Tags:  
  • Related