limit a user to only input values if it's in a previously given list

Viewed 91

i'm trying to make the user only pick values from a list and if he picks other values outside the list I want the code to ask again for the correct value

my attempt :

accepted_values = [0, 1, 2, 3, 4]
while True:
            
    print('Enter two numbers from the list :', accepted_values)
    first_num = input("Enter first_num: ")
    second_num = input("Enter second_num: ")
    userinput = []
    userinput.append(int(first_num))
    userinput.append(int(second_num))
    if userinput not in accepted_values:
        print('please enter numbers from this range:', accepted_values)
        continue
    else:
        break

but it results in a loop of it asking me to enter the correct values even if I enter a correct number

5 Answers

You're trying to check if [1, 2] is in accepted_values list. The integers 1 and 2 are SEPERATLY but the literal list [1, 2] isn't. Below is a way around it:

accepted_values = [0, 1, 2, 3, 4]
    while True:
        print('Enter two numbers from the list :', accepted_values)
        first_num = input("Enter first_num: ")
        second_num = input("Enter second_num: ")
        userinput = []
        userinput.append(int(first_num))
        userinput.append(int(second_num))

        # Using "any"
        if any(num not in accepted_values for num in userinput):

            print('please enter numbers from this range:', accepted_values)
            continue
        else:
            break

Using the any statement, you can sort of iterate through the user input while checking if its in accepted values.

You are checking if the entire list userinput is an element of accepted_values. So that means it checks whether [input1, input2] is in [0, 1, 2, 3, 4] instead of asking whether input1 and input2 are in [0, 1, 2, 3, 4]. The simplest way to correct it is to change if userinput not in accepted_values: to if userinput[0] not in accepted_values or userinput[1] not in accepted_values:.

I see perfect usecase for python sets here. Something like this will fit your needs perfectly:

>>> accepted_values = [1,2,3,4]
>>> user_input = [1,4]
>>> set(user_input) <= set(accepted_values )
True
>>>

Your condition checking is wrong. Here is a solution to check by using set:

if not (set(userinput).issubset(set(accepted_values))):

If the above condition is True, means userinput is not a subset of accepted_values.

just move your append method to the else statement:

accepted_values = [0, 1, 2, 3, 4]
while True:
    
    print('Enter two numbers from the list :', accepted_values)
    first_num = input("Enter first_num: ")
    second_num = input("Enter second_num: ")
    
    if first_num not in accepted_values or second_num not in accepted_values:
        print('please enter numbers from this range:', accepted_values)
        continue
    else:
        userinput = []
        userinput.append(int(first_num))
        userinput.append(int(second_num))
        break
Related