定义存款循环

2024-09-27 21:27:15 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行代码时,deposit循环给我一个错误,说明我没有定义deposit。这和读写文件有什么关系吗?我的代码似乎是对的,有人能帮忙吗?任何帮助都将不胜感激。谢谢。在

import time
import pickle
print("Hello, Welcome to Shawn's Bank ATM")
print("")
print("Please begin with creating an account")
name=input("Enter your name: ")
phone=input("Enter your phone number: ")
address=input("Enter your address: ")
code=input("Please enter a 4 digit pin to use as your passcode: ")

print()
print("Your account summary is:")
print("Name:" + name)
print("Phone Number:" + phone)     
print("Address:" + address)
print("Pin Code:" + code)    
print()

personalData = open("Personal data.txt","w")
personalData.write("Your name is " + name + "\n")
personalData.write("Your address is " + address + "\n")
personalData.write("Your phone number is " + phone + "\n")
personalData.write("Your password is " + code + "\n")           #the program will write the users information to a file, which can be called up by choosing one of the options
personalData.close()

history = open("History.txt","w")
history.close()                                                       #opens all files then closes them, easier to write to them when they are already made 
personalData_1 = open("Personal data Checking Account.txt","w")
personalData_1.close()
personalData_2 = open("Personal data Savings Account.txt","w")
personalData_2.close()

balance_1=float(input("Enter an amount to deposit into the savings account: "))
balance=float(input("Enter an amount to deposit into the chequing account: "))

print()
print(name,", Thank you for creating an account.")
def printMenu():
    print()
    print("Please choose an option below:")
    print("""
    Enter b to Check your Balance
    Enter d to Deposit money into your Account
    Enter w to Withdraw money from your Account
    Enter t to transfer money from chequing to savings
    Enter s to transfer money from savings to chequing
    Enter h to view your transaction history
    Enter q to Quit the Program """)
    print("")

def getTransaction():
    transaction=str(input("What would you like to do? "))
    return transaction

def withdraw_1(balance_1,amount):
    global balance
    balance=bal-amt
    if balance<0:
        balance=balance-10

def formatCurrency(amt):
    return "$%.2f" %amt

###MAIN PROGRAM###

printMenu()
command=str(getTransaction())

while command!="q":


    if (command=="b"):
        print(name,"Your current balance in your savings account is",formatCurrency(balance_1 ))
        print(name,"Your current balance in your chequing account is",formatCurrency(balance))
        printMenu()
        command=str(getTransaction())

    elif (command=="d"):      
        deposit=input("Press 1 for savings account//Press 2 for chequing account")
    if deposit=='1':
        amount=float(input("Amount to deposit into savings account? "))
        balance_1=balance_1+amount
        history = open("History.txt","w") 
        history.write(time.strftime("%c") + ": $" + (str(depositing) + " deposit into savings.\n"))
        history.close()
        printMenu()
        command=str(getTransaction())
    if deposit=='2':
        amount=float(input("Amount to deposit into chequing account? "))
        balance=balance+amount
        history = open("History.txt","w") 
        history.write(time.strftime("%c") + ": $" + (str(depositing) + " deposit into chequing.\n"))
        history.close()
        printMenu()
        command=str(getTransaction())

    elif (command=="w"):
        withdraw=input("Press 1 for savings account//Press 2 for chequing account")
        if withdraw=='1':
            amount=float(input("Amount to withdraw from savings account? "))
            withdraw_1(balance_1,amount)
            history = open("History.txt","w") 
            history.write(time.strftime("%c") + ": $" + (str(amount) + " withdrawal from savings.\n"))
            history.close()
        if withdraw=='2':
            amount=float(input("Amount to withdraw from chequing account? "))
            withdraw_1(balance,amount)
            history = open("History.txt","w") 
            history.write(time.strftime("%c") + ": $" + (str(amount) + " withdrawal from chequing.\n"))
            history.close()
            printMenu()
            command=str(getTransaction())

    elif (command=="h"):
        history = open("History.txt","r")
        print(history.read())
        history.close() 

    elif (command=="t"):
        transfer = float(input("Enter the amount you would like to transfer from your chequing account to your savings account ")) 
        balance_1 = balance - transfer
        print("Your balance in your savings account now is $", balance)
        print()
        print("Your balance in your Chequing Account now is $", balance)           #will take away money from checking account, and add to the savings account, will also write to the file aswell
        personalData_2 = open("Personal data Savings Account.txt","w")
        personalData_2.write("Your new balance is $" + (str(balance)))
        personalData_2.close()
        history = open("History.txt","w") 
        history.write(time.strftime("%c") + ": $" + (str(amount) + " has been transferred from chequing to savings account.\n"))
        history.close()
        printMenu()
        command=str(getTransaction())

    elif (command=="s"):
        transfer_1 = float(input("Enter the amount you would like to transfer from your Savings Account to Chequing Account "))
        balance_1 = balance + transfer_1
        print("Your balance in your checking account now is $", balance_1)
        print()                                                                       #will take away money from savings account, and add to the checking account, will also write to the file aswell
        print("Your balance in your Savings account now is $", balance) 
        personalData_1 = open("Personal data Checking Account.txt","w")
        personalData_1.write("Your new balance is $" + (str(balance))) 
        personalData_1.close()
        history = open("History.txt","w") 
        history.write(time.strftime("%c") + ": $" + (str(amount) + " has been transferred from savings to chequing account.\n"))
        history.close()
        printMenu()
        command=str(getTransaction())


else:
        print("Incorrect command. Please try again.")
        printMenu()
        command=str(getTransaction())



print(name,"Goodbye! See you again soon")

Tags: toinputyouraccountamounthistorycommandwrite
2条回答

您检查余额或存款,然后检查存款选择(储蓄或支票),而不管用户是否实际选择了“存款”。缩进相关块,使其仅在用户选择“存款”时出现:

elif (command=="d"):      
    deposit=input("Press 1 for savings account//Press 2 for chequing account")
    if deposit=='1':
        ...
    elif deposit=='2':
        ...

您将deposit用作整数,但在引用对象之前(在项目开始时)从未定义过该对象

 deposit = 0

相关问题 更多 >

    热门问题