以下是有问题的功能:
def ATM():
global mode
pinNum = input('Please enter your 4 digit secret code: ')
userBalance = float(dict2[pinNum])
while mode == 0:
if pinNum in dict1:
greet = input('Hello {}, please enter 1 to check your balance, 2 to make a withdrawal, 3 to make a deposit, or 4 to end your session: '.format(dict1[pinNum]))
if greet == '1':
balance(userBalance)
elif greet == '2':
withdraw(userBalance)
elif greet == '3':
deposit(userBalance)
elif greet == '4':
mode = 1
def balance(userBalance):
print('Your current balance is {}.'.format(userBalance))
def deposit(userBalance):
amount = input('Please enter the amount you wish to be deposited: ')
userBalance += float(amount)
return userBalance
def withdraw(userBalance):
amount = input('Please enter the amount you wish to withdraw" ')
if userBalance - float(amount) < 0:
print('You do not have sufficient funds.')
else:
userBalance -= float(amount)
当我在ATM()中调用存款或取款功能时,我无法调整余额。我想我可能无法在存款和取款功能中正确返回数据。该程序模拟ATM供参考,dict1和dict2在函数外定义。任何帮助表示赞赏。
答案 0 :(得分:1)
我认为这可以帮到你:
def ATM():
global mode
pinNum = input('Please enter your 4 digit secret code: ')
userBalance = float(dict2[pinNum])
actions = {
'1': balance,
'2': withdraw,
'3': deposit
}
while mode == 0:
if pinNum in dict1:
greet = input('Hello {}, please enter 1 to check your balance, 2 to make a withdrawal, 3 to make a deposit, or 4 to end your session: '.format(dict1[pinNum]))
if greet == '4':
break
else:
userBalance = actions.get(greet)(userBalance) or userBalance
def balance(userBalance):
print('Your current balance is {}.'.format(userBalance))
def deposit(userBalance):
amount = input('Please enter the amount you wish to be deposited: ')
userBalance += float(amount)
return userBalance
def withdraw(userBalance):
amount = input('Please enter the amount you wish to withdraw" ')
if userBalance - float(amount) < 0:
print('You do not have sufficient funds.')
else:
userBalance -= float(amount)
return userBalance