2012-03-22 106 views
1

我正在做這個家庭作業,我被困在我所知道的一個非常簡單的問題上。代碼的前提是小額現金系統,用戶可以在該系統中進行存款,取款和獲得當前餘額。 我的問題是,提款和存款似乎沒有通過我的帳戶類。這是一個全球變量問題嗎?我將在稍後添加時間戳。先謝謝你。可能的全局變量問題?

import datetime 
import time 

class Account: 
    def __init__(self, initial): 
     self.balance = initial 

    def deposit(self, amt): 
     self.balance = self.balance + amt 

    def withdraw(self,amt): 
     self.balance = self.balance - amt 

    def getbalance(self): 
     return self.balance 

def yesno(prompt): 
    ans = raw_input(prompt) 
    return (ans[0]=='y' or ans[0]=='Y') 

def run(): 
    done = 0 
    while not done: 
     user_options() 
     print 
     done = not yesno("Do another? ") 
     print 

def user_options(): 
    print ("Here are your options:") 
    print 
    print (" (a) Deposit cash") 
    print (" (b) Withdraw cash") 
    print (" (c) Print Balance") 
    print 
    u = raw_input("Please select a letter option: ").lower() 
    user_input(u) 

def user_input(choice): 
    account = Account(0.00) 
    if choice == "a" or choice == "b" or choice == "c": 

     if choice == "a": 
      d = input("Enter Deposit Amount: $") 
      account.deposit(d) 

     if choice == "b": 
      w = input ("Enter Withdraw Amount: $") 
      account.withdraw(w) 

     if choice == "c": 
      print ("Balance Amount: $"), 
      print account.getbalance() 

    else: 
     print ("Not a correct option") 


run() 

#now = datetime.datetime.now() 

Python版本:2.7.2

回答

1

的問題是:

account = Account(0.00) 

每次user_input被調用,您創建一個新的空賬。由於user_inputuser_options調用,在運行中while循環內部調用user_options,這發生在每次事務之前。

你需要移動線圈外的,例如:

def run(): 
    done = 0 
    global account # just showing you one way, not recommending this 
    account = Account(0.00) 
    while not done: 
     user_options() 
     print 
     done = not yesno("Do another? ") 
     print 
+0

這就是它!每次選擇用戶選項時,它都會將「account」重置爲「0.0」。我知道這是一個簡單的問題,但看不到它。謝謝。 – tw0fifths 2012-03-22 05:17:45

0

你不需要全局變量,只是重組代碼稍有這樣

def run(): 
    account = Account(0.00) 
    done = 0 
    while not done: 
     user_option = user_options() 
     user_input(user_option, account) 

     print 
     done = not yesno("Do another? ") 
     print 

def user_options(): 
    print ("Here are your options:") 
    print 
    print (" (a) Deposit cash") 
    print (" (b) Withdraw cash") 
    print (" (c) Print Balance") 
    print 
    return raw_input("Please select a letter option: ").lower() 

def user_input(choice, account): 
    if choice == "a" or choice == "b" or choice == "c": 

     if choice == "a": 
      d = input("Enter Deposit Amount: $") 
      account.deposit(d) 

     if choice == "b": 
      w = input ("Enter Withdraw Amount: $") 
      account.withdraw(w) 

     if choice == "c": 
      print ("Balance Amount: $"), 
      print account.getbalance() 

    else: 
     print ("Not a correct option") 


run() 

您仍然可以做更多的代碼更好,這只是足以讓帳戶部分工作

+0

這看起來像我的作業,這就是爲什麼我沒有給出完整的答案。 – agf 2012-03-22 05:15:06

+0

我在第一行表示這是一項家庭作業。我並不是要求人們爲我編寫代碼。還有很多部分要做;在我的生活中,我無法弄清楚爲什麼它一直給我'0.0'。 – tw0fifths 2012-03-22 05:19:26