So this is a prompt for user input, and it works just fine. I was printing some names and associated (1 based) numbers to the console for the user to choose one. I am also giving the option to quit by entering q.
The condition for the number to be valid is a) it is a number and b) it is smaller or equal than the number of names and greater than 0.
number = str(input("Enter number, or q to quit. \n"))
if number == "q":
number = int(number)
if number <= len(list_of_names) and number > 0:
name = list_of_names[number-1]
A bit simpler:
while True: choice = str(input("Enter number, or q to quit. \n")) if choice.lower() == "q": sys.exit() elif choice.isdigit() and (0 < int(choice) <= len(list_of_names)): name = list_of_names[int(choice)-1] break