Home > Enterprise >  Finding items in lists using the in operator
Finding items in lists using the in operator

Time:08-19

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?

CodePudding user response:

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!")

CodePudding user response:

I believe its a missing conversion when u append, its appended as a string and u search for an int

CodePudding user response:

Because the productNums list contains string values, not integer, so you always compare int with str variable and obtain the same result. Set productNums elements to int and it will work properly.

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!")

CodePudding user response:

Thank You, I resolved the issue

CodePudding user response:

You correctly cast nums and numFind to int, but you didn't do so for the input values in the while loop — so productNums is a list of single-character strings.

This change will fix it:

    productNums.append(int(input("Add a number: ")))  

CodePudding user response:

The problem is that you are trying to find an int in a list of strings.

When you wrote the code productNums.append(input("Add a number: ")), after the user enters the code, the number would be appended into the productNums list as a string.

Further on, the numFind = int(input("What number are you trying to find? ")) makes the user enter a number. this time, the input is converted into an int. So when the line of code if numFind in productNums: runs, it sees the if as the user trying to find an int in a list of strings.

In short, you should either change the productNums.append(input("Add a number: ")) code into productNums.append(int(input("Add a number: "))), or change the line of code numFind = int(input("What number are you trying to find? ")) into numFind = input("What number are you trying to find? ")

I hope this is helpful.

  • Related