patternpythonMinor
Determine the ratio of the contents of a pot to the (metal) volume of of the pot
Viewed 0 times
potthecontentsmetaldetermineratiovolume
Problem
This is my first Python project, so:
create a pot with dimensions (radius, thickness of wall, thickness of base, height), fill it with food (to a specific height). Determine the ratio between the volume of the food and the volume of the (metal in the) pot.
I am aware that this code does not take handles, rims, dents and other funny stuff into consideration.
create a pot with dimensions (radius, thickness of wall, thickness of base, height), fill it with food (to a specific height). Determine the ratio between the volume of the food and the volume of the (metal in the) pot.
I am aware that this code does not take handles, rims, dents and other funny stuff into consideration.
import math
pi = math.pi
class Pot:
"""create a pot with contents, determine the ratio between the contents and the pot"""
def __init__(self,inner_radius,wall_width,base_thickness,height,content_height):
self.wall_width = wall_width
self.inner_radius = inner_radius
self.outer_radius = self.inner_radius + self.wall_width
self.base_thickness = base_thickness
self.height = height
self.content_height = content_height
def get_cylinder_volume(self,radius,height):
cylinder_volume = pi * (radius ** 2) * height
return cylinder_volume
def get_pot_wall(self):
inner_cylinder = self.get_cylinder_volume(self.inner_radius,self.height)
outer_cylinder = self.get_cylinder_volume(self.outer_radius,self.height)
pot_wall = outer_cylinder - inner_cylinder
return pot_wall
def get_pot_base(self):
pot_base = self.get_cylinder_volume(self.inner_radius,self.base_thickness)
return pot_base
def get_pot_volume(self):
pot_volume = self.get_pot_base() + self.get_pot_wall()
return pot_volume
def get_content(self):
content = self.get_cylinder_volume(self.inner_radius,self.content_height)
return content
def get_ratio(self):
ratio = self.get_content()/self.get_pot_volume()
return ratio
def check_ratio(self,desired):
if (self.get_ratio() < desired):
print ("too small")
else:
print (self.get_ratio())Solution
You could get rid of many temporary variables, by returning the result right away:
Also, many of your variables are already determined when you initialize the class. Unless you want to change the shape of the pot on the fly (for this I would create a new instance of Pot with the new dimensions), the only thing I would expect to change is the amount of content in the pot. So, pot_wall, pot_base, pot_volume can all be calculated in init and set to variables of the instance:
This would allow you to get rid of
import math
pi = math.pi
class Pot:
"""create a pot with contents, determine the ratio between the contents and the pot"""
def __init__(self,inner_radius,wall_width,base_thickness,height,content_height):
self.wall_width = wall_width
self.inner_radius = inner_radius
self.outer_radius = self.inner_radius + self.wall_width
self.base_thickness = base_thickness
self.height = height
self.content_height = content_height
def get_cylinder_volume(self,radius,height):
return pi * (radius ** 2) * height
def get_pot_wall(self):
inner_cylinder = self.get_cylinder_volume(self.inner_radius,self.height)
outer_cylinder = self.get_cylinder_volume(self.outer_radius,self.height)
return outer_cylinder - inner_cylinder
def get_pot_base(self):
return self.get_cylinder_volume(self.inner_radius,self.base_thickness)
def get_pot_volume(self):
return self.get_pot_base() + self.get_pot_wall()
def get_content(self):
return self.get_cylinder_volume(self.inner_radius,self.content_height)
def get_ratio(self):
return self.get_content()/self.get_pot_volume()
def check_ratio(self,desired):
if (self.get_ratio() < desired):
print ("too small")
else:
print (self.get_ratio())Also, many of your variables are already determined when you initialize the class. Unless you want to change the shape of the pot on the fly (for this I would create a new instance of Pot with the new dimensions), the only thing I would expect to change is the amount of content in the pot. So, pot_wall, pot_base, pot_volume can all be calculated in init and set to variables of the instance:
def __init__(self,inner_radius,wall_width,base_thickness,height,content_height):
self.inner_radius = inner_radius
self.content_height = content_height
outer_radius = inner_radius + wall_width
self.pot_volume = self.get_cylinder_volume(inner_radius,base_thickness) + self.get_cylinder_volume(outer_radius,height) - self.get_cylinder_volume(inner_radius,height)
...
def get_ratio(self):
return self.get_content()/self.pot_volumeThis would allow you to get rid of
get_pot_wall, get_pot_base, get_pot_volume. Or at least the first two, if this is too much inline calculation for you.Code Snippets
import math
pi = math.pi
class Pot:
"""create a pot with contents, determine the ratio between the contents and the pot"""
def __init__(self,inner_radius,wall_width,base_thickness,height,content_height):
self.wall_width = wall_width
self.inner_radius = inner_radius
self.outer_radius = self.inner_radius + self.wall_width
self.base_thickness = base_thickness
self.height = height
self.content_height = content_height
def get_cylinder_volume(self,radius,height):
return pi * (radius ** 2) * height
def get_pot_wall(self):
inner_cylinder = self.get_cylinder_volume(self.inner_radius,self.height)
outer_cylinder = self.get_cylinder_volume(self.outer_radius,self.height)
return outer_cylinder - inner_cylinder
def get_pot_base(self):
return self.get_cylinder_volume(self.inner_radius,self.base_thickness)
def get_pot_volume(self):
return self.get_pot_base() + self.get_pot_wall()
def get_content(self):
return self.get_cylinder_volume(self.inner_radius,self.content_height)
def get_ratio(self):
return self.get_content()/self.get_pot_volume()
def check_ratio(self,desired):
if (self.get_ratio() < desired):
print ("too small")
else:
print (self.get_ratio())def __init__(self,inner_radius,wall_width,base_thickness,height,content_height):
self.inner_radius = inner_radius
self.content_height = content_height
outer_radius = inner_radius + wall_width
self.pot_volume = self.get_cylinder_volume(inner_radius,base_thickness) + self.get_cylinder_volume(outer_radius,height) - self.get_cylinder_volume(inner_radius,height)
...
def get_ratio(self):
return self.get_content()/self.pot_volumeContext
StackExchange Code Review Q#134461, answer score: 3
Revisions (0)
No revisions yet.