0

I know there are similar topics and I checked every single one but I still can't figure out what I'm doing wrong here:

I have a list (todos) and want to search for an ID with the following function:

def searchID():
    todo_id_search = input('Please input ID:')
    if todo_id_search in todos:
        print(todo_id_search)
    else:
        print("ID not in list")

Somehow it never returns the ID (even though its in the list). Should be possible to search for the ID like that or ma I thinking wrong? I'm using Python 3.7.

Thanks!

lemon
  • 14,875
  • 6
  • 18
  • 38
user10826235
  • 69
  • 1
  • 6
  • You should include a language tag (probably [tag:python]) – Patrick Haugh Dec 27 '18 at 16:47
  • Welcome to Stack Overflow! Questions about a particular piece of code can best be answered when there is enough of it for us to copy, run, and get your current result: a [mcve]. Then we can offer fixes. So. What is in `todos`? – Jongware Dec 28 '18 at 11:38
  • The underlying problem was probably something like https://stackoverflow.com/questions/20449427, but the question as asked is not suitable for the site as it does not contain any of the necessary information to debug the code. – Karl Knechtel May 29 '22 at 23:43

1 Answers1

0

I think this is caused due to the datatype mismatch between your input data and elements of the list that you are checking with.

for example: if the elements of list in your todos are integer data, then

todo_id_search = input('Please input ID:')

this todo_id_search is always a string as input always returns string type data hence failing your if block of code as string and int cannot be compared,For troubleshooting you can check its type by using

print (type(todo_id_search))

and after confirming your list todos elements type you can perform the necessary type casting in user input data i.e todo_id_search, by using int(todo_id_search) or float(todo_id_search) in your if condition.

ShivaGaire
  • 2,283
  • 1
  • 20
  • 31