
Bryce H.
asked 01/23/23Code troubleshooting
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
1 Expert Answer
Ebony C. answered 01/24/23
Current software developer with 4+ years of professional experience
Each of the output lines if the fruit is not found, the print statement is missing an extra space which causes the error. You have the if statements inside of the for loop which is fine but you don't want to check if the fruit is not found and print that each time. Rather you would want to check each item in the inventory and at the end if it is found then return/break and print.
Depending on the required output or desired behavior you want to store or keep track of the fruit found and you can print outside of the loop or you can just print once at the end, outside of the loop, if the count is 1 or is 0. Rather than printing the Fruit_name variable you want to use Fr the loop iterator.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Bradley T.
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.01/24/23