首页 > 解决方案 > 运行一次选择后,IF 循环无法正常工作

问题描述

我正在尝试运行一个交互式会话,用户可以在其中选择要做什么。用户选择所需模块后,所选模块将运行并返回选择页面。但是,如果用户再次选择相同的模块,IF 循环就会出错。有人知道我的代码出了什么问题吗?请参阅下面的代码以获取更多详细信息。

def comparison_select():
    global month1, month2
    while True:
        try:
            comparison_menu()
            compare_input = int(input("Enter selected comparison: "))
            if compare_input == 1:
                print("Selected comparison period is: Daily")
                from compare_exp_daily import command_daily
            elif compare_input == 2:
                print("Selected comparison period is: Monthly")
                from compare_exp_monthly import command_monthly
            elif compare_input == 0:
                print("Thank you & Goodbye!")
                break
            else:
                print("Error, please enter a number from the menu.\n"
                      "Thank you!")
        except:
            print("Error, please enter a valid selection.")

第一个输出工作正常,但随后当用户选择相同的选择时,它会出错。

输出:

    Comparison Available:         
1. Daily
2. Monthly
0. Exit


Enter selected comparison: 1
Selected comparison period is: Daily

Enter Date (DD/MM/YYYY): 12/07/2021

Enter Date (DD/MM/YYYY): 16/07/2021
-------------------------------------------------------------------------------
Expense for 2021-07-12 is 15.0 dollars.
Expense for 2021-07-16 is 21.0 dollars.
-------------------------------------------------------------------------------
You have spent 6.0 dollars more on 2021-07-16 than on 2021-07-12

Comparison Available:         
1. Daily
2. Monthly
0. Exit


Enter selected comparison: 1
Selected comparison period is: Daily

Comparison Available:         
1. Daily
2. Monthly
0. Exit


Enter selected comparison: 1
Selected comparison period is: Daily

Comparison Available:         
1. Daily
2. Monthly
0. Exit


Enter selected comparison: 0
Thank you & Goodbye!

标签: loopsif-statement

解决方案


对于我在您的代码中可以看到的唯一解释是,您总是导入一个函数而不是之前导入它(应该在哪里完成),然后总是在 if 语句上运行它。由于导入只运行一次,因此它只运行一次(这感觉很奇怪,因为它是导入后运行的)

我会将导入更改为函数之前(因为它们是全局导入)并在 if 语句中调用函数,如下所示:

#imports
from compare_exp_daily import command_daily
from compare_exp_monthly import command_monthly



def comparison_select():
   global month1, month2
   while True:
       try:
           comparison_menu()
           compare_input = int(input("Enter selected comparison: "))
           if compare_input == 1:
               print("Selected comparison period is: Daily")
               command_daily()
           elif compare_input == 2:
               print("Selected comparison period is: Monthly")
               command_monthly
           elif compare_input == 0:
               print("Thank you & Goodbye!")
               break
           else:
               print("Error, please enter a number from the menu.\n"
                     "Thank you!")
       except:
           print("Error, please enter a valid selection.") 

此外,我觉得有一种更好的方法来检查菜单中是否有有效的选择,方法是使用 if 语句而不是 try ,除了:

#imports
from compare_exp_daily import command_daily
from compare_exp_monthly import command_monthly



def comparison_select():
    global month1, month2
    while True:
        comparison_menu()
        compare_input = int(input("Enter selected comparison: "))
        if compare_input == 1:
            print("Selected comparison period is: Daily")
            command_daily()
        elif compare_input == 2:
            print("Selected comparison period is: Monthly")
            command_monthly
        elif compare_input == 0:
            print("Thank you & Goodbye!")
            break
        elif compare_input !=0 && compare_input!= 1 && compare_input!=2:
            print("Error, please enter a number from the menu.\n Thank you!")
         

还有一件事。虽然 True 有效,但我会使用 while exit !=1 ,因为它的内存效率更高,并且并不总是做事的最佳方式。您应该在 if compare_input == 0 条件中将该退出变量更新为 1,例如:

elif compare_input == 0:
            print("Thank you & Goodbye!")
            exit = 1

如果您想使用此解决方案,请确保退出变量位于 while 循环之外,并将其与 1 以外的任何其他值一起使用。

对我的回答有任何疑问,请向我寻求帮助:)


推荐阅读