Reputation: 589
from pip._vendor.distlib.compat import raw_input
Inventory1 = ["Rice", "Maze", "Oil", "Toothpaste", "Beans", "Cloth", "Pepper"]
print("Here are present items list information:", Inventory1)
CheckInv1 = input('Is this item present in the inventory: \n')
for CheckInv1 in Inventory1:
if CheckInv1 == ("Rice", "Maze", "Oil", "Toothpaste", "Beans", "Cloth", "Pepper"):
print("This item is present in the inventory list.")
break
else:
print("User entered item is not present in the list.")
break
Question: Even enter "Oil" text in code input, its still showing else print statement. If statement is not compared. Any suggestions. Thanks
Upvotes: 0
Views: 63
Reputation: 3066
You are trying to compare an item with a tuple. This are different kinds, so if always evaluated as false.
if CheckInv1 in ("Rice", "Maze", "Oil", "Toothpaste", "Beans", "Cloth", "Pepper"):
will check if what inside CheckInv1
is an element of your tuple.
Also, notice you are disregarding from the input. inside for loop (for CheckInv1 in Inventory1:
) CheckInv1
is an item of the Invenory1
you defined earlier and not user input.
If your intention was to check if user input is inside inventory, you should remove your for loop. If you want to do it for couple of times, use a flag (like boolean) to keep looping as long as it evaluated to true and ask user for another input inside the loop:
from pip._vendor.distlib.compat import raw_input
Inventory1 = ["Rice", "Maze", "Oil", "Toothpaste", "Beans", "Cloth", "Pepper"]
print("Here are present items list information:", Inventory1)
CheckInv1 = input('Is this item present in the inventory: \n')
conLoop = True
while conLoop:
if CheckInv1 in Inventory1:
print("This item is present in the inventory list.")
conLoop = False
else:
print("User entered item is not present in the list.")
CheckInv1 = input('Is this item present in the inventory: \n')
this code keeps asking for user input until one of the products is in the inventory
Upvotes: 1
Reputation: 12927
Sorry to say, this code is all wrong:
You overwrite the value of CheckInv1
that came from input
,
because you reuse the same name for for loop control variable;
You test CheckInv1
for equality against a tuple of values, but
CheckInv1
is never going to be a tuple, so equality is not
possible;
You put break
in both branches of your if
, so no matter if the
if
condition is true or not, your loop will end after first
iteration.
What you really need to do is quite simple:
Inventory1 = ["Rice", "Maze", "Oil", "Toothpaste", "Beans", "Cloth", "Pepper"]
print("Here are present items list information:", Inventory1)
CheckInv1 = input('Is this item present in the inventory: \n')
if CheckInv1 in Inventory1:
print("This item is present in the inventory list.")
else:
print("User entered item is not present in the list.")
Upvotes: 1