I have a data validation function:
# Function to validate user input
def get_valid_choice(i):
while True:
try:
user_input = int(input("Enter the number of your choice: "))
if 1 <= user_input <= len(i):
return user_input
else:
print(f"Please enter a number between 1 and {len(i) - 1}.")
except ValueError:
print("Invalid input. Please enter a number.")
which is working fine when im sending it the length of a list in one function (choose_race) but not in the other similar function (choose_gender), and im not sure why. It's saying choose between 1 and 3, but the list only has 2 items in it.
def choose_race():
#TODO from races.py list races dict and make a choice
print("What Race are you?")
# Display numbered choices
for index, i in enumerate(race_list, start=1):
print(f"{index}. {i['race_name']}")
# Get and validate user choice
selected_index = get_valid_choice()
selected_choice = race_list[selected_index - 1]
return selected_choice
def choose_gender():
gender_choices = ['Male', "Female"]
print(f"length of gender_choices is: {len(gender_choices)}")
print("Which gender are you?")
print(gender_choices) # shows 2 entries in list at index positions 0 and 1
# Display numbered choices
for index, i in enumerate(gender_choices, start=1): # start at 1 instead of index 0, real start
print(f"{index}. {i}")
# Get and validate user choice
selected_index = get_valid_choice()
selected_choice = gender_choices[selected_index - 1] # minus 1 to get actual index position
return selected_choice