
Andrew K.
asked 12/16/20Write a program in python that calculates the amount of paint needed to paint a room.
A room has one door, two windows, and a built-in bookshelf and it needs to be painted. Suppose one gallon of paint can paint 120 square feet. Write a program that prompts the user to input the lengths and widths of the door, each window, the bookshelf; and the length, width, and height of the room(in feet). The program outputs the amount of paint needed to paint the walls of the room.
2 Answers By Expert Tutors

Aaron T. answered 12/17/20
Princeton Grad for Computer Science Tutoring
I'll provide a sketch of how one might approach this.
In order to take user input in Python, you'd typically use the input() function. In Python 3, this returns the string value entered by the user, so it will need to be checked for validity and cast to a float. You can wrap the common logic for this in a function to avoid re-implementing it for each input.
Once you have the dimensions, you can use basic math to calculate the total area of the room walls, then subtract the area of the door, bookshelf, and two windows. This will be the area to be painted. Dividing the area to be painted (in square ft) by 120 gives you the number of gallons of paint needed.
Since this is modeling a physical space, it seems reasonable to check that the door, windows, and bookshelf will actually fit inside the room. This is trickier than the portion above. The program must keep track of the state the walls (whether they have objects on them, how much space is free and in what dimensions), account for different mappings of objects -> walls (e.g. one object each on four walls vs. two objects each on two walls), and ensure it assigns objects to walls in a way that conserves the available space (so that if there is a way to fit all the objects in the room, the program finds it rather than failing and rejecting the user's inputs).
I would recommend writing out a number of test cases to get a sense of the different values (valid and invalid) the program might encounter and going from there to implement and test your algorithm for this latter problem. Two suggestions:
- Pay attention to your search order when matching objects to available wall space
- Consider keeping track of state in data structures, such as a Python class
Hope this helps!

Patrick B. answered 12/17/20
Math and computer tutor/teacher
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
print("Hello world")
door_width = input('Please input door width :>')
door_height = input('Please input door height :>')
window1_width = input('Please input 1st window width :>')
window1_height = input('Please input 1st window height :>')
window2_width = input('Please input 2nd window width :>')
window2_height = input('Please input 2nd window height:>')
bookshelf_width = input('Please input bookshelf width:>')
bookshelf_height = input('Please input bookshelf height:>')
room_width = input('Please input room width :>')
room_height = input('Please input room height :>')
room_length = input('Please input room length:>')
door_area = float(door_width) * float(door_height)
window1_area = float(window1_width)*float(window1_height)
window2_area = float(window2_width)*float(window2_height)
bookshelf_area = float(bookshelf_width)*float(bookshelf_height)
# are we painting the floor?
wall_area1 = float(room_width)*float(room_height)
wall_area2 = float(room_width)*float(room_length)
wall_area3 = float(room_height)*float(room_length)
print("door area = " + str(door_area))
print(" area of 1st window is " + str(window1_area))
print(" area of 2nd window is " + str(window2_area))
print(" area of bookshelf is " + str(bookshelf_area))
print(" wall area #1 = " + str(wall_area1))
print(" wall area #2 = " + str(wall_area2))
print(" wall area #3 = " + str(wall_area3))
surface_area = 2*(wall_area1 + wall_area2 + wall_area3)
print(" surface area = " + str(surface_area))
paintable_area = surface_area - window1_area - window2_area - bookshelf_area - door_area
paint_gallons = round(paintable_area/120)
print(" paintable area = " + str(paintable_area))
print(" # of gallons of paint needed = " + str(paint_gallons))
price_per_gallon = input(' please input price per gallon :> ')
total_cost = float(price_per_gallon) * float(paint_gallons)
print(" cost = " + str(total_cost))
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.
Patrick B.
source code uploaded to RESOURCES section under this link12/17/20