jeudi 30 septembre 2021

IF loop not working correctly after running selection once

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!

Aucun commentaire:

Enregistrer un commentaire