0
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

deadshot
9,0774 gold badges23 silver badges40 bronze badges
asked May 17, 2020 at 13:17
2
  • 1
    CheckInv1 isn't the tuple you are comparing it to; it may be contained in the tuple, though. You want in, not ==. Commented May 17, 2020 at 13:21
  • you are comparing string with the entire tuple Commented May 17, 2020 at 13:21

2 Answers 2

1

Sorry to say, this code is all wrong:

  1. You overwrite the value of CheckInv1 that came from input, because you reuse the same name for for loop control variable;

  2. You test CheckInv1 for equality against a tuple of values, but CheckInv1 is never going to be a tuple, so equality is not possible;

  3. 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.")
answered May 17, 2020 at 13:24
0
1

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

answered May 17, 2020 at 13:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.