0

This is the function, it takes in the user's input, and a list of valid inputs and checks if the user input (userin) is contained in the list (valid_inputs). If it is, then it prints both (for bug checking) and returns the original input. Otherwise it prints out an error message and lets the user try again, after 5 attempts it stops.

def exception_catch_opt(userin, valid_options):         
#catches when use selects an invalid option
    import sys
    tries = 5
    for attempt_no in range(tries):
        if userin in valid_options:
            print(userin)
            print(valid_options)
            return userin
        else:
            userin = input("Please select a valid option")

This is the line I'm using to call the function, it defines the input and the valid options list, and prints out different strings for each option.

if exception_catch_opt(input("1     Yes\n2     No\n"), [1, 2]) == 2:
        print("Until next time!")
    else:
        print("Until next time2!")

The problem is when I run the above command, I receive the input text, but no matter what I respond with, I get "Please select a valid option" error message.

I'm at a bit of a loss because from what I see, if I enter a "2" it should print

'2'
[1, 2]
'Until next time!'

But all I get is

Please Select a valid option
Tomerikoo
  • 15,737
  • 15
  • 35
  • 52
  • Does this answer your question? [TypeError: unsupported operand type(s) for -: 'str' and 'int'](https://stackoverflow.com/questions/2376464/typeerror-unsupported-operand-types-for-str-and-int) TL;DR `input` ***always*** returns a string and you're checking if it is in a list of ints... – Tomerikoo Dec 08 '20 at 17:55
  • input is always a string, I'm dumb – Qestroy Dec 08 '20 at 18:16
  • lol you're not dumb, you didn't know (?). Anyway don't beat yourself up, hope that solves your problem – Tomerikoo Dec 08 '20 at 18:18

0 Answers0