仍然是Python的初学者。 7周为8周的课程。我的任务是完善以前的任务。我要添加的内容不是必需的,但我认为始终将最佳用户界面作为目标是一种很好的做法。我目前有一个功能正常的ATM脚本,可以运行一次,然后必须重新启动才能使用其他功能之一。我想做的是以某种方式调用我创建的user_options词典,以提示用户查看他们是否要在ATM程序仍在运行时执行另一项任务。
为澄清起见,我希望程序能够启动,提示用户,运行用户选择的功能,然后循环回提示以进行其他输入。
如果将user_options dict更改为函数,我将不知道该怎么做。无论如何,对不起,这是我的代码:
import sys
account_balance = float(500.25)
# defines theprintbalance function
def balance():
print("Your current balance:\n" + str(account_balance))
#defines the deposit function
def deposit():
global account_balance
#input paramaters for deposit
deposit_amount = input("How much would you like to deposit today?\n")
#modifies account balance with deposit ammount
account_balance += float(deposit_amount)
#prints the deposit amount and current blanace
print("Deposit was $" + str('%.2f' % float(deposit_amount)) + ", current
balance is $" + str(account_balance))
#defines withdrawal function
def withdrawal():
global account_balance
#input paramaters for withdrawal
withdrawal_amount = input("How much would you like to withdraw today?\n")
#if statement for withdrawal amount that exceeds balance
if float(withdrawal_amount) > account_balance:
print("$" + str('%.2f' % float(withdrawal_amount)) + " is greater than
your account balance of $" + str('%.2f' % float(account_balance)))
#restarts the withdrawl
print('Please enter an amount less than or equal to your current balance
for withdrawal')
withdrawal()
#else statement for successful withdrawal
else:
account_balance -= float(withdrawal_amount)
print("Withdrawal amount was $" + str('%.2f' % float(withdrawal_amount))
+ ", current balance is $" + str('%.2f' % float(account_balance)))
#defines quit function
def quit():
#prints quit message
print("Thank you for banking with us.")
#creates user input dict and removes previously used if/else loop
user_options = input("What would you like to do? (B)alance, (W)ithdrawal,
(D)eposit, (Q)uit\n")
options = {'B': balance,
'b': balance,
'W': withdrawal,
'w': withdrawal,
'D': deposit,
'd': deposit,
'Q': quit,
'q': quit,
}
options[user_options]()
如果有人有想法或建议,那就太好了。这绝对是一件我想添加到程序中的装饰性物品。我认为我的评分不会有任何不同。我只想学习。
答案 0 :(得分:0)
创建一个while循环,其中将包含它们具有的所有选项。一旦他们完成了选择要做的任何选择,就跳出循环并将用户发送回程序的开头,在这里他们可以选择退出ATM或运行其他选项。