I have a somewhat stupid question which drives me nuts for a couple of days now. Heavy web search did not help either.
Within some larger SQLAlchemy application I want to check whether some value exists inside a list or not (before the SQL is executed). Therefore I have defined two functions: one which checks the existence of a value inside a list, and gives back True or False. Plus a second one which loops as long as the value is not in the list:
def check_value(itemlist, value_check):
print(itemlist) #only for debugging reasons
if value_check in itemlist:
print('Item is already in list') #only for debugging reasons
return False
else:
print('Item is not in list') #only for debugging reasons
return True
def check_input(itemlist, value_check):
while check_value(itemlist ,value_check)==False:
value_check = input('Please input valid value')
return value_check
now if I run this code lets say:
def check_value(itemlist, value_check):
print(itemlist) #only for debugging reasons
if value_check in itemlist:
print('Item is already in list') #only for debugging reasons
return False
else:
print('Item is not in list') #only for debugging reasons
return True
def check_input(itemlist, value_check):
while check_value(itemlist ,value_check)==False:
value_check = input('Please input valid value')
return value_check
if __name__ == "__main__":
items = [1, 18272, 18279, 12, 298]
value_check = 18272
check_input(items, value_check)
IDLE gives me the correct answer at first:
[1, 18272, 18279, 12, 298]
Item is already in list
Please input valid value
Then I give it a number that is also in the list let's say "1" but it still tells me the value is not in the list:
[1, 18272, 18279, 12, 298]
Item is not in list
>>>
Its clear that this has been asked several times and I have found some help here already:How to check if a specific integer is in a list I tried this method x is in (see code above).
Also I found this thread: Finding the index of an item given a list containing it in Python which would be my workaround to check for index of a given value and write some try/except around it. Nevertheless I do not understand why x is y is not working here.
I have the feeling I am missing something very basic here but I am not able to grasp what.