2015-11-19 52 views
1

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

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

您的存款的缩进如果您的存款错误 –

+0

严重吗?你能告诉我如何正确缩进吗?谢谢 – user5583688

+0

你的代码中没有“存款循环”。 – kindall

回答

0

您检查是否有余额或存款,但你检查什么存款的选择是(储蓄或检查),而不管用户是否实际选择“存款”。缩进相关的块,这样,只有当用户选择“存款”出现:

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

您使用的保证金,作为一个整数,但被引用之前,你从来没有定义的对象(在开始)

deposit = 0