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.
01/24/23