Write a simple bank account system in Python! It's really strong!

 

 

The main method involved in the article is open(filename,'r') in Python to open the file in reading open(filename,'w') to open the file in writing. We use for * in * to read the data in the file Or write file data and use the dict(eval(list2)) method to convert the string into a dictionary. The
detailed code is as follows

# 刘文豪
# 大帅哥
import math 
import re 

def main(): # Main function 
    select = True 
    while (select): 
        menu() 
        start_int = input("Please select the serial number of the function you want to operate:") 
        if start_int = = "12": 
            select = False 
            print("You have already exited the system and welcome to come next time") 
        elif start_int == "4": 
            insert() 
        elif start_int == "5": 
            login() 
        elif start_int == "6 ": 
            show() 
        elif start_int == "11": 
            delete() 
        elif start_int == "7":
            revise()
        elif start_int == "8":
            deposit()
        elif start_int == "9": 
            getMoney() 
        elif start_int == "10": 
            UseMoney() 



def menu(): # Menu display 
    print("1========Bank deposit and withdrawal system=== =====") 
    print("2===========================") 
    print("3====== =====Function menu===========") 
    print("4=========Register personal information==========") 
    print( "5============Login=============") 
    print("6=========Query personal information==== ======") 
    print("7=========Modify personal account==========") 
    print("8========== ==Save money=============") 
    print("9============Withdraw money============= ") 
    print("10=========Display annual income==========")12===========Exit=============") 
filename = "Bank.txt" # Define the file name for saving user information) 
    print("11========Cancel personal information==========")
    print("12===========Exit=============") 
            deposit = int(input("Enter the amount you want to deposit"))



def save(Bank): # Create file method 
    try: 
        Bank_txt = open(filename, "a") 
    except Exception as e: 
        Bank_txt = open(filename, "w") 
    for info in Bank: 
        Bank_txt.write(str(info) + "\n") 
    Bank_txt.close() 


def insert(): # Registration method 
    BankList = [] # Save user information list 
    mark = True # Whether to continue adding 
    while mark: 
        id = input("Please enter your ID password ( Such as 1001): ") 
        if not id: 
            break 
        name = input("Please enter your name") 
        if not name: 
            break 
        try: 
                break 
        except:
            if deposit == 0:
            print("The input is invalid, not an integer, please re-enter") 
            continue 
        Bank = {"id": id, "name": name, "deposit": deposit} 
        BankList.append(Bank) 
        mark = False 
    save( BankList) 
    print("Registered successfully") 


global g_select 
g_select = 0 
global Username 
global Userpassword 


def login(): # Login method 
    global Username 
    global g_select 
    global g_BankQuery 
    global Userpassword 
    g_BankQuery = [] 
    Username = str(input("Please enter your user Name")) 
    Userpassword = str(input("Please enter your password")) 
    file = open(filename,'r') 
    Bank = file.readlines() # read all content 
    for list in Bank:
        d = dict(eval(list)) # Characters are converted into a dictionary 
        if d['name'] == Username and d['id'] == Userpassword: 
            g_BankQuery.append(d) 
            print("Login successful!") 
            g_select = 1 
        else: 
            pass 
    if not g_BankQuery: 
        g_select = 0 
        print("Login failure, please register first!") 
    else: 
        pass 




def show(): # Query personal information 
    if g_select == 1: 
        format_title = "{:^6}{ :^12}" 
        print(format_title.format("name", "deposit")) 
        format_date = "{:^6}{:^12}"  
        for info in g_BankQuery :
            print(format_date.format(str(info.get('name') ), str(info.get('deposit')))) 
    else: 
        print("Please login first!")


def delete(): # Delete personal account method 
    global g_BankQuery 
    cz = [] 
    global g_select 
    choose = 0 
    if g_select == 1: 
        while choose <3: 
            username = str(input("Please enter your name")) 
            userpassword = str( input("Please enter your password")) 
            file = open(filename,'r') 
            Bank = file.readlines() # read all content 
            for list in Bank: 
                    for list2 in Bank: 
                        d2 = dict(eval(list2)) # Characters are converted to dictionaries 
                d = dict(eval(list)) # characters are converted to dictionary 
                if d['name' ] == username and d['id'] == userpassword:
                    cz.append(d)
                    file.close() 
                    choose = 3 
                    NewBank = open(filename,'w') # Open the file by writing 
                        if d2['name'] != username and d2['id'] != userpassword: 
                            NewBank.write( str(d2) + "\n") 
                        else: 
                            pass 
                else: 
                    pass 
            if not cz: 
                choose = choose + 1 
                if choose == 3: 
                    g_select = 0 
                    print("Please log in again!") 
                else: 
                    print("Username Or the password is wrong, please re-enter you still have:"+ str(3-choose) + "opportunity")
            else: 
                for list2 in Bank: 
                g_BankQuery.clear() 
                g_select = 0 
                print("Your personal information has been cancelled")

    else: 
        print("Please log in first!") 

def revise(): # Modify personal account method 
    cz = [] 
    global g_select 
    if g_select == 1: 
        username = input("Please enter your username:") 
        userpassword = input ("Please enter your password:") 
        file = open(filename,'r') 
        Bank = file.readlines() # read all contents 
        for list in Bank: 
            d = dict(eval(list)) # characters are converted to Dictionary 
            if d['name'] == username and d['id'] == userpassword: 
                cz.append(d 
                ) 
                file.close () NewBank = open(filename,'w') # Open the file by writing 
                    d2 = dict(eval(list2)) # characters are converted into dictionaries 
                    if d2['name'] == username and d2['id'] == userpassword: 
                        d2['name'] = input("Enter your new name :") 
                        d2['id'] = input("Enter your new password:") 
                        NewBank.write(str(d2) + "\n") 
                        print("Modified successfully, please log in again!") 
                        g_select = 0 
                    else: 
                        NewBank.write(str(d2) + "\n") 
            else: 
                pass 
        if not cz: 
            print("The password or username you entered is incorrect, please log in again") 
            g_select = 0 
        else:
            pass
    else:
        print("Please log in first!") 
                    if d2 ['name'] == Username and d2['id'] == Userpassword:
) 
def deposit(): # 
    Deposit method global g_BankQuery
    global g_select 
    cz = [] 
    if g_select == 1: 
        money = int(input("Please enter how much money you want to deposit:")) 
        file = open(filename,'r') 
        Bank = file.readlines() # read All content 
        for list in Bank: 
            d = dict(eval(list)) # Characters are converted to dictionaries 
            if d['name'] == Username and d['id'] == Userpassword: 
                cz.append(d) 
                file. close() 
                NewBank = open(filename,'w') # Open the file by writing 
                for list2 in Bank: 
                    d2 = dict(eval(list2)) # Convert characters into a dictionary 
                        NewBank.write(str(d2) + "\n")
                        d2['deposit'] = str(int(d2['deposit']) + money) 
                        print("Save successfully!") 
                        g_BankQuery.clear() 
                        g_BankQuery.append(d2) 
                    else: 
                        NewBank.write(str(d2) + "\n") 
            else: 
                pass 
    else: 
        print("Please login first!") 

def getMoney(): # 
    Withdraw money method global g_select 
    global g_BankQuery 
    cz = [] 
    if g_select == 1: 
        money = int(input(" Please enter how much money you want to withdraw:")) 
        file = open(filename,'r') 
        Bank = file.readlines() # read all content 
        for list in Bank: 
            d = dict(eval(list)) # characters are converted to dictionary 
            if d['name'] == Username and d['id'] == Userpassword: 
                cz.append(d) 
                if money> int(d['deposit']): 
                    print("Your balance is insufficient") 
                else: 
                    file.close 
                    () NewBank = open(filename,'w') # Open the file by writing 
                    for list2 in Bank: 
                        d2 = dict(eval(list2)) # Characters are converted to dictionaries 
                        if d2['name'] == Username and d2['id '] == Userpassword: 
                            d2['deposit'] = str(int(d2['deposit'])-money) 
                            NewBank.write(str(d2) + "\n") 
                            print(" Withdraw money successfully!")
                            g_BankQuery.clear() 
                            Withdraw g_BankQuery.append(d2)
                        else:
                            NewBank.write(str(d2) + "\n") 
            else: 
                pass 
    else: 
        print("Please log in first!") 


def UseMoney(): # Interest calculation 
    UM = True 
    while UM: 
        try: 
            money = float(input( "Please enter how much money you want to invest in financial management:")) 
            year = int(input("Please enter how many years you want to store:")) 
        except: 
            print("Please enter an integer year!") 
        if 0 <year < = 3: 
            profitmargin = 0.03 
        elif 3 <year <= 5: 
            profitmargin = 0.04 
        elif 5 <year <= 10: 
            profitmargin = 0.08
        if money < 0 or year <= 0: 
            profitmargin = 0.06
        elif year> 10: 
if __name__ =="__main__ ":
            print("Your principal cannot be less than 0 yuan or the year cannot be less than 0 years") 
        else: 
            UM = False 
            profit = round(money * year * profitmargin, 3) 
            print("You save:" + str(year) + "The annual profit will be equal to:" + str(profit) + "yuan principal plus profit will be equal to:" + str(profit + money) + "yuan")


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289

Running picture:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Recently, many friends consulted about Python learning issues through private messages. To facilitate communication, click on the blue to join the discussion and answer resource base by yourself

 

Guess you like

Origin blog.csdn.net/weixin_43881394/article/details/113046256