我希望它,使我目前的余额将作为下一个循环提取的金额。
输入密码:123 1 -余额查询2 -取款3 -存款X -退出输入您的选择:2嗨姓名1.
您当前余额为50000
输入要提取的金额:400交易成功
您当前的余额为49600
输入密码:123 1 -余额查询2 -取款3 -存款X -退出输入您的选择:2嗨姓名1.
您当前的余额是50000***问题***
输入要提取的金额:
这是目前我的代码。(抱歉混乱的代码,因为我是一个初学者)
pin = [123, 456, 789]
balance = [50000, 2000, 500]
name = ["name1", "name2", "name3"]
def main():
while True:
pin_input = input('Enter pin:')
try:
n = int(pin_input)
except:
break
if n in pin:
print('1 – Balance Inquiry')
print('2 – Withdraw')
print('3 – Deposit')
print('X – Exit')
choice = input('Enter your choice:')
c = int(choice)
if choice == 'X':
print('Thank you for banking with us!')
break
else:
pol = pin.index(n)
if c == 1:
print(f'Hi {name[pol]}.')
print(f'Your current balance is {balance[pol]} ')
elif c == 2:
print(f'Hi {name[pol]}.')
print(f'Your current balance is {balance[pol]} ')
withdraw = int(input('Enter amount to withdraw: '))
if withdraw > balance[pol]:
print('Not enough amount')
else:
difference = balance[pol] - withdraw
print('Transaction Successful')
print(f'Your current balance is {difference}')
elif c == 3:
print(f'Hi {name[pol]}.')
print(f'Your current balance is {balance[pol]} ')
deposit = int(input('Enter amount to deposit: '))
sums = deposit + balance[pol]
print('')
print(f'Your current balance is {sums}')
main()
1条答案
按热度按时间lymnna711#
欢迎使用Python。我知道你的问题是什么了。当你处理撤销时,你创建了一个新的变量,并执行了减法,你只是显示了新的结果,而没有更新它。所以要解决这个问题,你需要替换下面的代码:
与:
我冒昧地编辑了你的代码,使它更“专业”,但也使你可以阅读和理解它(我添加了评论,让你阅读)。
"干得好,继续干下去!"
另外,我想添加我自己的创建ATM机的方法。我希望有一天当你学习并有更多的知识时,你会再次打开这个并尝试理解它。(这个代码只在py-3.10或更高版本中工作)
代码: