2

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?

3
  • Typo. You are converting numFind to int, but you have left the productNums as strings. Commented Aug 18, 2022 at 20:03
  • productNums.append(input("Add a number: ")) is the problem here - you don't cast the input to an int, so it gets added to productNums as a string, and then when you ask for numFind, you cast it to an int so you're trying to find an int among a list of strings. Commented Aug 18, 2022 at 20:03
  • productNums.append(int(input("Add a number: "))) should do it Commented Aug 18, 2022 at 20:15

5 Answers 5

1

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!")
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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

Comments

0

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? ").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.