Bryce H.

asked • 01/23/23

Code troubleshooting

Fruit_List = ["apple", "pear", "peach", "banana"]
Fruit_Name = input("Enter Fruit of Choice Here: ")
Fruit_Count = 0
for Fr in Fruit_List:
if Fr.lower() == Fruit_Name.lower():
print(Fruit_Name + " is in the inventory")
Fruit_Count = 1
break
if Fruit_Count == 0:
print(Fruit_Name + "is not in the inventory")

Why does my code not print what is inventory and what is not.

Enter Fruit of Choice Here: Banana

Bananais not in the inventory

Bananais not in the inventory

Bananais not in the inventory

Banana is in the inventory


Process finished with exit code 0

It just prints this

Bradley T.

tutor
The counter and the two ifs are not doing what you expect them to do. Additionally you do not need to loop through the entire list. You should look up the "in" keyword in python. That will allow you to get rid of the loop. And you do not need the Fruit_count variable that acts like a Boolean. You can use if, elif, else instead. I would suggest looking for a tutor because you have asked a lot of broad questions, and I think a tutor would be able to better explain. It is difficult to explain everything you need without that back and forth you and a tutor could have.
Report

01/24/23

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.