Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding items in lists using the in operator

I created some code where I try to find elements in a list given by the user using the in operator.

productNums = []

nums = int(input("How many numbers should be in your list?"))

while nums > 0:
    productNums.append(input("Add a number: "))  
    nums -= 1

numFind = int(input("What number are you trying to find? "))

if numFind in productNums:
    print("This product number is in the list")
elif numFind not in productNums:
    print("Ahh, sorry. This product number is NOT in this list!")
else:
    print("Huh, you're confusing me now, check your numbers pal!")

When I run this, I get the outcome that the number is not in this list, even though it is in it. Can someone tell me what I am doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As people said above, just small issue with you integers and strings.
This is your code working:
Be careful with the indentention, initially was a mess.

productNums = []

nums = int(input("How many numbers should be in your list?"))

while nums > 0:
    productNums.append(int(input("Add a number: ")))  
    nums -= 1

    numFind = int(input("What number are you trying to find? "))

    if numFind in productNums:
      print("This product number is in the list")
    elif numFind not in productNums:
      print("Ahh, sorry. This product number is NOT in this list!")
    else:
      print("Huh, you're confusing me now, check your numbers pal!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading