Home > Software design >  IF loop not working correctly after running selection once
IF loop not working correctly after running selection once

Time:10-04

I am trying to run an interactive session where user is able to choose from selection what to do. After user selects the module they want, selected module will run and return back to selection page. However, if user selects the same module again, the IF loop sort of goes wrong. Anybody knows what went wrong with my code? Please refer to the code below for more details.

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

The first output works fine but subsequently when user selects back the same selection, it goes wrong.

output:

    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!

CodePudding user response:

For what I can see with your code the only explanation I can see is that u are always importing a function instead of importing it before(where it should be done) and then run it always on the if statement. Since imports only run once it only runs it once(which feels weird it even running since its an import)

I would change the imports to before the function(since they are imports are global) and call the functions in the if statement as such:

#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.") 

Also I feel there is a better way to check if there is a valid selection from your menu, by using a if statement instead of a try except:

#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!")
         

One more thing. Although while True works, I would use a while exit !=1 because its more memory efficient and isn't always the best way to do things. You should update that exit variable to 1 in the if compare_input == 0 condition like:

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

If you wanna go to this solution please make sure that the exit variable is outside of the while loop and put it with any other value other than 1.

Any questions about my answer, please ask I m here to help :)

  • Related