Question
PYTHON: Currently there are some problems with the code. Consider the following situations: 1. The cashier is able to enter a negative number. This value
PYTHON:
Currently there are some problems with the code. Consider the following situations:
1. The cashier is able to enter a negative number. This value will be added to the total and count. While this is good for you as a customer, it is not so good for the store trying to make money.
2. If you enter zero the first time you are asked for a price, the loop will end, and the program will try to divide by zero.
Fix the code so that:
1. Modify the code so that negative numbers give an error message instead (but dont end the loop) Hint: elif is your friend.
2. Modify the code to use an if / else statement outside the loop to avoid the division by zero and notify the user that you cant compute an average without data.
3. This program doesnt display the amounts to two decimal places. Check out String Format Method in the next chapter; that will do the trick.
MY CODE:
def checkout(): total = 0 count = 0 moreItems = True while moreItems: price = float(input('Enter price of item (0 when done): ')) if price != 0: count = count + 1 total = total + price print('Subtotal: $', total) else: moreItems = False average = total / count print('Total items:', count) print('Total $', total) print('Average price per item: $', average)
checkout()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started