Treders
Treders

Reputation: 9

Code not checking values in list?

This program I am trying to build needs to check if the user's input corresponds to values in a list. Here is the code I have:

def find_val(val, seq):
    for ele in seq:
        if val == ele:
            return True
    return False

def get_input(possible_vals, day_or_time_string):
    if day_or_time_string == "day":
        answer = input("What day would you like your appointment? ")
    else:
        answer = input("What time would you like your appointment? ")
    answer = answer.strip()
    valid_entry = find_val(answer, possible_vals)
    if valid_entry == True:
        count = 0
        while False:
            second_answer = input("Invalid entry. Please enter a valid day: ")
            count = count + 1
            if count == 3:
                print("This is getting silly - still not a valid entry")
            if second_answer in possible_vals:
                return second_answer
    else:
        return answer

day_list = ["Monday", "Tuesday", "Wednesday"]
res = get_input( day_list, "day" )
print("The funtion returned", res)

This is the type of output I should be getting:

What day would you like your appointment? saturday
Invaild entry. Please enter a valid day: Monday
The funtion returned Monday

However, it seems that no matter what I input, the function returns it, and doesn't check if the input matches a string in the list:

What day would you like your appointment? akw
The function returned akw

What is wrong with my code that isn't allowing the user's input to be checked whether or not it is in the list day_list?

Upvotes: 0

Views: 44

Answers (1)

Julien
Julien

Reputation: 15071

First

valid_entry = find_val(answer, possible_vals)
if valid_entry == True:

can be simplified to

if answer in possible_vals:

(your find_val function is totally unnecessary)

Then your problem is that

while False: 
    ...

simply never executes... Did you mean while True? If so You'll need to somehow break out this (now) infinite loop using a break statement for example.

And finally as A.G. suggests, you explicitly tell your program to return answer when it's not "valid" so why do you expect otherwise?

Upvotes: 2

Related Questions