Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database Design With Microcomputer Applications

Authors: Glenn A. Jackson

1st Edition

0137718411, 978-0137718412

More Books

Students also viewed these Databases questions

Question

How would you assess the value of an approach like this?

Answered: 1 week ago

Question

When would you use one approach, and when would you use another?

Answered: 1 week ago

Question

3. How would this philosophy fit in your organization?

Answered: 1 week ago