1

I have started learning python about 2 weeks ago and am trying to create a password system that can store multiple usernames and passwords. I am trying to add a bit of code where it makes you wait a minute if you input a wrong password 3 times.

I keep on getting an error where if you get the password wrong 3 times and wait the 60 seconds, even if you do input the right password and username it prints "Your password is wrong. You have only 3 attempts left."

Can somebody help and point out flaws that I have in the code?

import time username = ["Test","Test2"] password = ["1Ac!","2Bd!"] success = 0 Tries = 0 Valid = False Pass = "" def login(): global success global Pass global Tries inuser = input(str("Enter your username: \n")) inpass = input(str("Enter your password: \n")) if inuser not in username: print("username not found in directory") login() else: posUser = username.index(inuser) Pass = password[posUser] while success != 1: if Tries == 3: print("You have had too many attempts. Wait 60 seconds before trying again") for i in range(60, 0, -1): time.sleep(1) print(i) if Tries == 3: Tries = 0 inuser=str("0") inuser=str("0") login() else: break if Pass == inpass: success=1 else: Tries += 1 AttemptsLeft = (3 - Tries)+ 1 print("Your password is wrong. You have only {} attempts left.".format(AttemptsLeft)) login() login() if success==1: print("Welcome to ------") 
1
  • Your pairing of usernames and passwords is only implicit, in that username[i] has password[i]. You can make that more explicitly, for example with something like accounts = [("Test", "1Ac!"), ("Test2", "2Bd!")] or accounts = {"Test": "1Ac!", "Test2": "2Bd!"}. Commented Jun 5, 2021 at 17:07

3 Answers 3

1

If you get rid of the recursion, everything will get simpler.

import time username = ["Test","Test2"] password = ["1Ac!","2Bd!"] success = 0 Tries = 0 Valid = False Pass = "" def login(): global success global Pass global Tries while success != 1: inuser = input(str("Enter your username: \n")) inpass = input(str("Enter your password: \n")) if inuser not in username: print("username not found in directory") else: posUser = username.index(inuser) Pass = password[posUser] if Tries == 3: print("You have had too many attempts. Wait 60 seconds before trying again") for i in range(60, 0, -1): time.sleep(1) print(i) Tries = 0 inuser=str("0") inuser=str("0") if Pass == inpass: success=1 elif Pass != "": Tries += 1 AttemptsLeft = (3 - Tries)+ 1 print("Your password is wrong. You have only {} attempts left.".format(AttemptsLeft)) login() if success==1: print("Welcome to ------") 

You should also consider getting rid of the global variables. Instead, return values using return.

import time username = ["Test","Test2"] password = ["1Ac!","2Bd!"] success = 0 Tries = 0 Valid = False Pass = "" def login(): success = 0 Pass = "" Tries = 0 while success != 1: inuser = input(str("Enter your username: \n")) inpass = input(str("Enter your password: \n")) if inuser not in username: print("username not found in directory") else: posUser = username.index(inuser) Pass = password[posUser] if Tries == 3: print("You have had too many attempts. Wait 60 seconds before trying again") for i in range(10, 0, -1): time.sleep(1) print(i) Tries = 0 inuser=str("0") inuser=str("0") if Pass == inpass: return True elif Pass != "": Tries += 1 AttemptsLeft = (3 - Tries)+ 1 print("Your password is wrong. You have only {} attempts left.".format(AttemptsLeft)) if login(): print("Welcome to ------") 
Sign up to request clarification or add additional context in comments.

Comments

0

Your code needed some refining. I also made a timer for stopping. Also, you should use getpass library to not show the password.

import time import getpass username = ["Test","Test2"] password = ["1Ac!","2Bd!"] Tries = 1 def login(): global Tries print("\n----------------- Login -----------------------\n") inuser = input("Enter your username: ") inpass = getpass.getpass("Enter your password: ") if inuser not in username: print(f"User {inuser} was not found.") login() else: xindex=username.index(inuser) passw=password[xindex] if inpass==passw: return True else: if Tries==3: print("\nToo many attempts. Please wait for 60 seconds.") for k in range(x,0,-1): b=f"Time left: {k} Seconds" print(b,end="\r") time.sleep(1) Tries=0 login() else: Tries+=1 print("\nWrong Password or Username.") login() x=login() if x: print("Welcome to ---------") 

Comments

0

Others have provided the solution, I will try to provide the reasoning:

This is because of the recursion in line number 38 where you're calling the login function after setting Tries=0.

When the recursive call is made, the compiler puts the rest of the statements below it on hold (you can imagine all the below statements pushed to a stack to be executed later). The program counter now goes to the start of the function: Where you'll enter the right username and password, therefore it sets the success variable to 1. Now that your function has completed, the compiler had a set of pending statements before remember?

It goes and executes them. But in there, the value of the variable inpass is still the old, incorrect one and that is why it prints out Wrong password. You can understand this by printing out inpass just before the line

if Pass == inpass: 

TlDr; Get rid of the recursion

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.