定义存款循环

时间:2015-11-19 23:38:03

标签: python

当我运行代码时,存款循环给我一个错误,说我没有定义存款。这与读取和写入文件有什么关系吗?我的代码似乎没错,有人可以帮忙吗?任何帮助将不胜感激。感谢。

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")

2 个答案:

答案 0 :(得分:0)

您检查余额或存款,但随后检查存款选择(节省或检查),无论用户是否实际选择了#34;存款。&#34;缩进相关的块,以便它们仅在用户选择&#34;存放&#34;:

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

答案 1 :(得分:0)

您使用存款作为整数,但在引用之前(在项目开始时)您从未定义过该对象。

 deposit = 0