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?
numFindtoint, but you have left theproductNumsas strings.productNums.append(input("Add a number: "))is the problem here - you don't cast the input to an int, so it gets added toproductNumsas a string, and then when you ask fornumFind, you cast it to anintso you're trying to find an int among a list of strings.productNums.append(int(input("Add a number: ")))should do it