I have to do an IT Project for School and we have this task, to make a login function and let people be able to withdraw cash from an imaginary bank. The problem is, that I don't know how to store a password, in the program as a variable, so that it doesn't revert to the original after changing it, between runs of the program. I also don't want to set up an external database to save the password. This is my code:
import getpass
IsBlocked = False
def login(): #Define the Login Fuction
attempts = 3
password = "password"
Success = False
while attempts > 0: #Start of a While Loop that Exits when the Login is Succesful
if attempts > 0:
try1 = getpass.getpass("Enter you Password: ")
if try1 == password:
print("Login Succesful, Welcome to the Bank!")
Success = True
break #Stops While
elif try1 != password:
print("Wrong password, try again.")
attempts = attempts - 1
print("You have ", attempts ," Attemtps left...") #Starts again until no more tries
if attempts == 0 and Success == False:
print("Login Blocked") #Locks the User out after too many tries.
IsBlocked = True
elif Success == True:
change = str(input("Do you want to change your password? (y/n): ")).lower().strip()
if change[0] == "y":
change1 = input("Enter new password. ")
change2 = input("Confirm new password. ")
if change1 == change2:
password = change2
withdraw()
elif change[0] == "n":
return password
def withdraw(): #Below is the Code to Withdraw Money, including the current balance
funds = 15000000
Exit = False
while Exit == False and IsBlocked == False:
amount = int(input("How much money do you want to withdraw?"))
if int(amount) < int(funds) and amount >= 0:
funds = funds - amount
if int(amount) % 10 == 0: #Only withdraws amounts, which are divisible by 10
print("Your updated Funds are now", funds, "€")
AskEx = str(input("Do you want to quit now? (y/n)")).lower().strip()
if AskEx[0] == "y":
print("OK Exiting... ")
break
elif AskEx[0] == "n":
print("Ok.")
else:
print("You can only withdraw amounts divisible by ten.")
else:
print("Insufficient Funds, Exiting... ")
break
login()
withdraw()