0
def get_choice():
    '''
    Repeatedly prompts user to enter a number between 1 and 5, inclusive, until user enters a valid number, then returns that number, as an integer
    '''
    y = input("Please enter the number of the option you'd like: ")
    while not (len(y) == 1 and '1' <= y <= '5'):
        y = input("That choice is not available. Please enter a number between 1 and 5: ")
    return int(y)


def main():
   
    while True:
        print("\nOptions\n"
              "1. calculate_average()\n"
              "2. letter_counter('brontasouruS')\n"
              "3. Display 'Option 3' selected\n"
              "4. Display 'Option 4' selected\n"
              "5. Quit\n")
        get_choice()
        
        if get_choice() == 1:
            calculate_average()
            
            
        elif get_choice() == 2:
            letter_counter('brontasouruS')
            
           
        elif get_choice() == 3:
            print("Option 3 selected")
            
            
        elif get_choice() == 4:
            print("Option 4 selected")
            
                    
        elif get_choice() == 5:
            print("Bye bye!")
            break
        
            
        
if __name__ == '__main__':
    print(TITLE)
    main()

This is my code, when I run it if I want to select option 3 I have to do it 3 times. Or if want to select option 5 I have to reenter it 5 times. desired selection be entered be entered several times?

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • 1
    `if get_choice() == 1:` means "**call `get_choice()` again** and see if the result is equal to 1". It will **not** use the result from the previous call. Similarly for the other branches. – Karl Knechtel Mar 10 '22 at 20:15

2 Answers2

2

Yes, because you are calling get_choice() multiple times. You should do:

        choice = get_choice()

        if choice == 1:
...
        elif choice == 2:
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

You keep calling get_choice() again and again, just store it, and use it

def main():
    while True:
        print("\nOptions\n"
              "1. calculate_average()\n"
              "2. letter_counter('brontasouruS')\n"
              "3. Display 'Option 3' selected\n"
              "4. Display 'Option 4' selected\n"
              "5. Quit\n")

        choice = get_choice()
        
        if choice == 1:
            calculate_average()
        elif choice == 2:
            letter_counter('brontasouruS')
        elif choice == 3:
            print("Option 3 selected")
        elif choice == 4:
            print("Option 4 selected")
        elif choice == 5:
            print("Bye bye!")
            break
azro
  • 53,056
  • 7
  • 34
  • 70