我正在做這個家庭作業,我被困在我所知道的一個非常簡單的問題上。代碼的前提是小額現金系統,用戶可以在該系統中進行存款,取款和獲得當前餘額。 我的問題是,提款和存款似乎沒有通過我的帳戶類。這是一個全球變量問題嗎?我將在稍後添加時間戳。先謝謝你。可能的全局變量問題?
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
這就是它!每次選擇用戶選項時,它都會將「account」重置爲「0.0」。我知道這是一個簡單的問題,但看不到它。謝謝。 – tw0fifths 2012-03-22 05:17:45