143k views
22 votes
Create a Python program that computes the cost of carpeting a room. Your program should prompt the user for the width and length in feet of the room and the quality of carpet to be used. A choice between three grades of carpet should be given. You should decide on the price per square foot of the three grades on carpet. Your program must include a function that accepts the length, width, and carpet quality as parameters and returns the cost of carpeting that room. After calling that function, your program should then output the carpeting cost. of carpeting a room. Note: the output is NOT in the function but in the main. Display your name,class,date as per SubmissionRequirements by using a function.

Your program should include your design steps as your comments. Document the values you chose for the prices per square foot of the three grades of carpet in your comments as well.

1 Answer

5 votes

Answer:

In Python:

def calcCost(length,width,quality):

if quality == 1:

cost = length * width * 3.50

if quality == 2:

cost = length * width * 2.50

else:

cost = length * width * 1.50

return cost

length = float(input("Room Length: "))

width = float(input("Room Width: "))

print("Quality:\\1 - Best\\2 - Moderate\\3 and others - Worst")

quality = int(input("Quality: "))

print("Calculate: "+str(calcCost(length,width,quality)))

Step-by-step explanation:

This defines the function calcCost to calculate the carpeting cost

def calcCost(length,width,quality):

If quality is 1, the total cost is calculated by area * 3.50

if quality == 1:

cost = length * width * 3.50

If quality is 2, the total cost is calculated by area * 2.50

if quality == 2:

cost = length * width * 2.50

For other values of quality, the total cost is calculated by area * 1.50

else:

cost = length * width * 1.50

This returns the calculated carpeting cost

return cost

The main begins here

This prompts the user for the length of the room

length = float(input("Room Length: "))

This prompts the user for the width of the room

width = float(input("Room Width: "))

This gives instruction on how to select quality

print("Quality:\\1 - Best\\2 - Moderate\\3 and others - Worst")

This prompts the user for the quality of the carpet

quality = int(input("Quality: "))

This calls the calcCost method and also printst the calculated carpeting cost

print("Calculate: "+str(calcCost(length,width,quality)))

User Paul Hiemstra
by
5.5k points