简体   繁体   English

Python中的ATM脚本

[英]Script for an ATM in Python

I am doing a project for class and can't figure out where I am going wrong. 我正在上课的项目,无法弄清楚我要去哪里。 The code works in sections, but when I run it all together it shuts down right after the first input. 该代码可在各部分中运行,但是当我一起运行所有代码时,它将在首次输入后立即关闭。

I think I need to call functions somewhere - but how and where? 我想我需要在某个地方调用函数-但是如何以及在哪里?

Below is all my code so far, with comments. 以下是我到目前为止的所有代码,并附有注释。

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()

This part should be as one userchoice = input ('What would you like to do? D for Deposit, W for Withdraw, B for Balance\\n') 这部分应该作为一个userchoice = input ('What would you like to do? D for Deposit, W for Withdraw, B for Balance\\n')

Not sure if you indented by accident, but python does not like that. 不知道您是否偶然缩进,但是python不喜欢这样。

Also, advice for your code. 另外,为您的代码提供建议。 Make it so user can either do uppercase or lowercase letters for input, also make sure it still grab input even if user put empty spaces after input string. 确保用户可以输入大写或小写字母,即使用户在输入字符串后放置空格也要确保它仍能抓取输入。 Your withdraw exit the program after entering the string character W. Balance is not grabbing the correct Deposit. 输入字符串字符W后,您的提款退出程序。余额未获取正确的存款。 Use for loops and condition to keep it looping and ask user when to exit. 使用循环和条件保持循环,并询问用户何时退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM