0

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()  
  • there is no mutch you can do, you need to store that information somewhere either a database, a .txt file, or even as pickle object – RomainL. Apr 23 '20 at 12:26
  • you have to set an extrnal "database" once the program finishes it is removed form emory, you need a file of some kind. there are many choices from plain text .txt files to pickle or sql etc... – Nullman Apr 23 '20 at 12:26
  • Does this answer your question? [Change hardcoded values in python 3](https://stackoverflow.com/questions/53388362/change-hardcoded-values-in-python-3) – MisterMiyagi Apr 24 '20 at 13:21

1 Answers1

-1

As RomainL and Nullmann suggested, you need external storage for this. A .txt file or a saved pickle is sufficient enough.

But why not learn something new which you'll probably can use over and over again in larger projects: SQL.

SQL is a Standard Query Language which in its core can be used for Hobbyist Databases to the big shots (Oracle, SQLServer, Postgres etc).

So my recommendation as a super lightweight and easy to install database is SQLite

Python does support it natively and you can get a good start here.

I hope that gets you started!

imolitor
  • 780
  • 6
  • 12