在同一循环中使用2个不同功能的1个数字选项

时间:2019-06-12 06:03:52

标签: python python-3.6

它是一个atm程序,选项2允许用户输入存款金额,但是在该存款选项中是使用相同数值的另一个选项列表 其他选项。

if verify_pin(pin):    
  print("           Welcome to MCC ATM           ")
  print("****************************************")
  print("*   1. Balance inquiry                 *")
  print("*   2. Deposit                         *")    
  print("*   3. Withdrawal                      *")    
  print("*   4. Transfer Funds                  *")    
  print("*   5. History of Last 5 Transactions  *")    
  print("*   6. Exit                            *")    
  print("****************************************")

  option = int(input())    
  if option == 6:        
    print("*Exited Program*")       
    break    
  elif option < 1:            
    print("***Invalid Entry***")    
  elif option > 6:           
    print("***Invalid Entry***")    
  elif option ==1: 
    #HERE YOU CAN SEE THAT OPTION 1 PRINTS THE BALANCE OF BOTH  ACCOUNTS
    print("Checkings Balance: $" + format(checkings,'.2f'))            
    print("Savings balance: $" + format(savings,'.2f'))    
  elif option == 2:
    #BUT HERE I NEED IT TO ALLOW THEM TO DEPOSIT INTO THE CHECKINGS ACOUNT WITHOUT PRINTING THE BALANCES
    print("*********************************************")            
    print("*  1. Checking Account                      *") 
    print("*  2. Savings Account                       *")  
    print("*  3. Go Back To Main Menu                  *")              
    print("*********************************************")

1 个答案:

答案 0 :(得分:1)

我建议您阅读有关Finite State machine的信息。

简而言之,最初,您的程序应处于main-menu状态。输入(2)之后,状态应更改为deposit并请求进一步的用户输入。此时,当用户输入(1)时,您应该忽略main-menu if / else块,而应检查deposit块。

因此,以您的代码为基础的伪代码解决方案如下所示:

while (app_running):
  if verify_pin(pin):
    if app_state == 'main-menu':
      # print main menu
      # request input
      # process input in the main menu if/else case
    if app_state == 'deposit':
      # print deposit menu
      # request input
      # process input in the deposit if/else case
    ...