Python中的ATM脚本

Python中的ATM脚本,python,Python,我正在为班级做一个项目,不知道哪里出了问题。代码是分段运行的,但是当我一起运行它时,它会在第一次输入后立即关闭 我想我需要在某个地方调用函数,但是如何调用以及在哪里调用呢 下面是我到目前为止的所有代码,并附有注释 import sys #account balance account_balance = float(500.25) ##prints current account balance def printbalance(): print('Your current bal

我正在为班级做一个项目,不知道哪里出了问题。代码是分段运行的,但是当我一起运行它时,它会在第一次输入后立即关闭

我想我需要在某个地方调用函数,但是如何调用以及在哪里调用呢

下面是我到目前为止的所有代码,并附有注释

import sys

#account balance 
account_balance = float(500.25)

##prints current account balance
def printbalance(): 
   print('Your current balance: %2g'% account_balance)

#for deposits 
def deposit(): 
  #user inputs amount to deposit
  deposit_amount = float(input()) 
  #sum of current balance plus deposit
  balance = account_balance + deposit_amount 
  # prints customer deposit amount and balance afterwards
  print('Deposit was $%.2f, current balance is $%2g' %(deposit_amount, 
balance))

#for withdrawals
def withdraw(): 
  #user inputs amount to withdraw
  withdraw_amount = float(input()) 
  #message to display if user attempts to withdraw more than they have
  if(withdraw_amount > account_balance):
    print('$%.2f is greater than your account balance of $%.2f\n' % 
(withdraw_amount, account_balance)) 
  else:
    #current balance minus withdrawal amount
    balance = account_balance - withdraw_amount 
    # prints customer withdrawal amount and balance afterwards
    print('Withdrawal amount was $%.2f, current balance is $%.2f' % 
(withdraw_amount, balance)) 

#system prompt asking the user what they would like to do
userchoice = input ('What would you like to do? D for Deposit, W for 
Withdraw, B for Balance\n')
if (userchoice == 'D'): #deposit
  print('How much would you like to deposit today?')
  deposit()
elif userchoice == 'W': #withdraw
  print ('How much would you like to withdraw today?')
elif userchoice == 'B': #balance
  printbalance()
else:
  print('Thank you for banking with us.')
  sys.exit()

此部分应作为一个
userchoice=input('您想做什么?D表示存款,W表示取款,B表示余额\n')

不确定是否意外缩进,但python不喜欢这样

另外,还有一些关于代码的建议。确保用户可以输入大写或小写字母,也确保即使用户在输入字符串后放置空格,它仍能抓取输入。 在输入字符串W后退出程序。 余额未获取正确的存款。
使用for循环和条件使其保持循环,并询问用户何时退出

欢迎来到StackOverflow。请按照您创建此帐户时的建议,阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。您只需将输入提示和if语句移动到while循环中,以便它多次发出请求。此时,它将询问一次,运行命令,然后在到达脚本末尾时退出