我是Python编程的初学者,因此我遵循的课程是每周必须提交作业。
目前的任务是编写一个程序,用户可以打开,关闭和帐户,可以存入和提取资金,并列出帐户(即根据特定帐户查看所有帐户和余额)。
我的主要问题是每个帐户的余额(从0开始)都是相同的,而每个帐户都应该有自己的余额!
我走到这一步:
# -*- coding: utf-8 -*-
def print_menu():
print "1. List Account(s)"
print "2. Open an Account"
print "3. Close an Account"
print "4. Withdraw money"
print "5. Deposit Money"
print "6. Quit"
print
accounts = {}
menu_choice = 0
print_menu()
#balance = {}
balance = 0
while True:
menu_choice = int(input("Type in a number (1-6): "))
print
if menu_choice == 1:
print "Accounts"
for x in accounts.keys():
print "Accountnumber:", x, "with balance €", balance
print
elif menu_choice == 2:
print "Enter a new Accountnumber"
number = input("New accountnumber: ")
#phone = input("Number: ")
accounts[number]=balance
print "Accountnumber", number, "opened."
elif menu_choice == 3:
print "Close an Accountnumber"
number = input("Accountnumber: ")
if number in accounts:
#accounts[number]=balance
del accounts[number]
print "Accountnumber", number, "is closed."
else:
print "Accountnumber", number, "was not found"
#elif menu_choice == 4:
#print("Lookup Number")
#name = input("Name: ")
#if name in numbers:
# print("The number is", numbers[name])
#else:
# print(name, "was not found")
elif menu_choice == 4:
print "Withdraw money from Account."
number = input("Accoutnnumber: ")
if number in accounts:
withdraw = float(input("How much money would you like to withdraw? > "))
if withdraw < balance:
#accounts[number]=balance
numbers[balance] -= withdraw
#balance vann number 444 bijv. !!
print "Your new balance is €", balance
else:
print "There are no sufficient funds on this accountnumber"
elif menu_choice == 5:
print "Deposit money onto Account."
number = input("Accountnumber: ")
if number in accounts:
deposit = float(input("How much money would you like to deposit? > "))
#accounts[number]=balance
balance += deposit
#balance vannn number 444 bijv. !!
print "Your new balance is €", balance
else:
print "That accountnumber does not exist."
elif menu_choice == 6:
print "Quit."
break
else:
print "Please enter a number between 1 and 6."
print
print_menu()
# Ook nog ff instellen dat wanneer input geen iets anders is,
# dan gewoon netjes afluisten, geen rare foutcodes !!
答案 0 :(得分:1)
问题是你正在使用&#34;平衡&#34;以错误的方式。如果您使用像字典这样的帐户,那么您会有更好的帐号和#34;和&#34;平衡&#34;。
这样,如果您有类似的内容:
accounts = {1:100, 2:1500, 3:0}
这意味着账号为1的人有100美元,第二个有2号账号的人有1500,依此类推。
例如,在选择4中,您正在执行此操作:
elif menu_choice == 4:
print "Withdraw money from Account."
number = input("Accoutnnumber: ")
if number in accounts:
withdraw = float(input("How much money would you like to withdraw? > "))
if withdraw < balance:
#accounts[number]=balance
numbers[balance] -= withdraw
#balance vann number 444 bijv. !!
print "Your new balance is €", balance
else:
print "There are no sufficient funds on this accountnumber"
但这更好:
elif menu_choice == 4:
print "Withdraw money from Account."
number = input("Accoutnnumber: ")
if number in accounts:
withdraw = float(input("How much money would you like to withdraw? > "))
balance = accounts[number]
if withdraw < balance:
accounts[number] -=withdraw
print "Your new balance is €", accounts[number]
else:
print "There are no sufficient funds on this accountnumber"
我希望这对你的作业有所帮助。