2012-03-22 10 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_inputは、実行時に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

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

+0

私はこれが宿題であると最初の行で述べました。私は人々が私のためにコードを書いて終了するように求めるものではありません。それでもやるべきことはたくさんあります。私は、私の人生のためになぜ私に「0.0」を与え続けているのか理解できませんでした。 – tw0fifths

関連する問題