Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON LOOPS I need help with Questions 7,8,9 and 10. For number 7, i'm unsure what the error is so please help with that as

PYTHON LOOPS
I need help with Questions 7,8,9 and 10. For number 7, i'm unsure what the error is so please help with that as well if my explanation is wrong if you can think of another solution. Thank you.
image text in transcribed
7. Provide comments for the following code: (5 points) \#Assigns the value 0 to a new variable called total. total =0 \#Assigns the value 0 to a new variable called count. count =0 \#Asks for user input of an item price, casts it to fioat, but will skip while loop when price is 0 . price = float (input ("Enter the price of iten (0 when done) : ")) ARuns loop body when price float was set to pos tive values, breaks out of loop when price entered is 0 . \#Precondition, price >=0. while price > ot \#increments the count of items by 1 . count = count +1 \#increments the total variable by the recorded price float. total += price \#Asks for user to update the value of price, or set to 0 when all items and prices are accounted. price = float (input (nenter the price of item (0 when done): ) )) \#Prints the total price as a String. print ("Total is: \$" + str (total)) \#Prints the total number of items accounted for in the while loop. print ("Number of items is. ", oount) Prints the average price by dividing the total price by the number of items. print ("Average price is: ", total ( count) The above code raises an error in a special case. 8. What is the error? ( 1 point) If a user inputs a negative value, the code will return an error because it will assume The average price as 0/0 and will crash Python since that is not possible. 9. What is the error type? (1 point) It is a runtime/exception error with ZeroDivisionError because if someone inputs a negative value, the console will print 0/0. 10. How do you prevent it? Write the part of the code that corrects it. (1 point) ng code accounts egative or aneous inputs

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

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

Students also viewed these Databases questions