patternpythonModerate
Paint cost calculator
Viewed 0 times
costpaintcalculator
Problem
This is the task I was given after completing my program. I don't have a clue what procedures and functions are to be honest. Therefore I have no clue on how to break down my program into different elements of it.
#paint calculator
RoomWidth = 0
RoomHeight = 0
RoomTotal = 0
RoomPaint = 0
RoomWidth = float(input("Enter the room width, it must be between 1m-25m: "))
RoomHeight = float(input("Enter the room height, it must be between 2m-6m: "))
if ((RoomWidth =1) and (RoomHeight =2)):
RoomTotal = ((RoomWidth * RoomHeight)*4)
PaintSelection = str (input("Please choose from a selection of paints: 'Luxury', 'Standard', or 'Economy'."))
if PaintSelection == "Luxury":
RoomPaint = RoomTotal * 1.45
elif PaintSelection == "Standard" :
RoomPaint = RoomTotal * 1.00
elif PaintSelection == "Economy" :
RoomPaint = RoomTotal * 0.45
UndercoatAnswer = str (input("Would you like to use an undercoat? Y/N: "))
if UndercoatAnswer == "Y":
UndercoatingCost = (RoomTotal * 0.5)
elif UndercoatAnswer == "N":
UndercoatingCost = 0
TotalCost = RoomPaint + UndercoatingCost
print ("The total cost of painting this room is £",TotalCost,)
else:
print ("Invalid input")Solution
Functions are described in the Python tutorial - they are separate blocks of code which take defined inputs (argument values) and provide defined outputs (
As to how you can proceed on refactoring your code to use these functions, I suggest you take the following steps:
Step 1: Abstract repeated code into standalone functions:
You can use this community wiki to help you write the function
Step 2: Use dictionaries to simplify multiple
Step 3: Organise whole thing into sensible sub-functions that each do one thing:
Step 4: Write one function to bring the rest together:
Step 5: Add a call at the bottom of your script to set the whole thing off:
(see this question if you don't know what that comparison does).
Throughout: Follow the Python style guide, PEP-0008.
return values), and usually have a descriptive comment ("docstring") at the top:def function(argument):
"""A simple function to double things."""
output = argument * 2
return outputAs to how you can proceed on refactoring your code to use these functions, I suggest you take the following steps:
Step 1: Abstract repeated code into standalone functions:
def get_float_input(prompt, min_, max_):
"""Take valid float input between min_ and max_ from the user."""
...
room_width = get_float_input("Enter the room width, it must be between 1m-25m: ", 1, 25)
room_height = get_float_input("Enter the room height, it must be between 2m-6m: ", 2, 6)You can use this community wiki to help you write the function
get_float_input.Step 2: Use dictionaries to simplify multiple
elifs, and continue to factor out repeats:def get_str_input(prompt, choices):
"""Take valid string input from the user from items in choices."""
...
prompt = "Please choose from a selection of paints: 'Luxury', 'Standard', or 'Economy'."
paint_factor = {"Luxury": 1.45, "Standard": 1.0, "Economy": 0.45}
room_paint = room_total * paint_factor[get_str_input(prompt, paint_factor)]
prompt = "Would you like to use an undercoat? Y/N: "
undercoats = {"Y": 0.5, "N": 0}
room_undercoat = room_total * undercoats[get_str_input(prompt, undercoats)]Step 3: Organise whole thing into sensible sub-functions that each do one thing:
def room_size():
"""Calculate the size of a room from user input."""
width = get_float_input(...)
height = get_float_input(...)
return width * height
def paint_cost(size):
"""Calculate the cost of paint for a room."""
prompt = "..."
paint_factor = {...}
return size * paint_factor[get_str_input(prompt, paint_factor)]
def undercoat_cost(size):
"""Calculate the cost of undercoat for a room."""
prompt = "..."
undercoats = {...}
return size * undercoats[get_str_input(prompt, undercoats)]Step 4: Write one function to bring the rest together:
def main():
size = room_size()
total_cost = paint_cost(size) + undercoat_cost(size)
print("The total cost of painting this room is £{0:.2f}".format(total_cost))Step 5: Add a call at the bottom of your script to set the whole thing off:
if __name__ == "__main__":
main()(see this question if you don't know what that comparison does).
Throughout: Follow the Python style guide, PEP-0008.
Code Snippets
def function(argument):
"""A simple function to double things."""
output = argument * 2
return outputdef get_float_input(prompt, min_, max_):
"""Take valid float input between min_ and max_ from the user."""
...
room_width = get_float_input("Enter the room width, it must be between 1m-25m: ", 1, 25)
room_height = get_float_input("Enter the room height, it must be between 2m-6m: ", 2, 6)def get_str_input(prompt, choices):
"""Take valid string input from the user from items in choices."""
...
prompt = "Please choose from a selection of paints: 'Luxury', 'Standard', or 'Economy'."
paint_factor = {"Luxury": 1.45, "Standard": 1.0, "Economy": 0.45}
room_paint = room_total * paint_factor[get_str_input(prompt, paint_factor)]
prompt = "Would you like to use an undercoat? Y/N: "
undercoats = {"Y": 0.5, "N": 0}
room_undercoat = room_total * undercoats[get_str_input(prompt, undercoats)]def room_size():
"""Calculate the size of a room from user input."""
width = get_float_input(...)
height = get_float_input(...)
return width * height
def paint_cost(size):
"""Calculate the cost of paint for a room."""
prompt = "..."
paint_factor = {...}
return size * paint_factor[get_str_input(prompt, paint_factor)]
def undercoat_cost(size):
"""Calculate the cost of undercoat for a room."""
prompt = "..."
undercoats = {...}
return size * undercoats[get_str_input(prompt, undercoats)]def main():
size = room_size()
total_cost = paint_cost(size) + undercoat_cost(size)
print("The total cost of painting this room is £{0:.2f}".format(total_cost))Context
StackExchange Code Review Q#52626, answer score: 11
Revisions (0)
No revisions yet.