简体   繁体   English

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

[英]using 1 numeric option for 2 different functions in the same loop

its an atm program, option 2 lets user enter a deposit amount but within that deposit option is is another list of options that uses the same numeric values for other options. 它是一个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("*********************************************")

I suggest you read about the Finite State machine . 我建议您阅读有关有限状态机的文章

In short, initially, your program should be in main-menu state. 简而言之,最初,您的程序应处于main-menu状态。 after you enter (2), the state should change to deposit and request further user input. 输入(2)后,状态应更改为“ deposit并请求进一步的用户输入。 At that moment, when the user enters (1), you should ignore the main-menu if/else block and instead check the deposit block. 这时,当用户输入(1)时,您应该忽略main-menu if / else块,而应检查deposit块。

thus a pseudo code solution using your code as a base would look something like: 因此,以您的代码为基础的伪代码解决方案如下所示:

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
    ...

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

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