For my project, I have all the calculations correct but for the one part where you have to calculate the percentages used and wasted it always gives either 0% or a something along the lines of 1000%. How do I correct this?
Code
-----------------------------------------------------------------------------------------------
print ("Welcome to the Cake Slice Program!")
cake_type = str(input("What kind of cake did you make?"))
length = int(input(" How long is the cake in centimeters?"))
width = int(input(" How wide is the cake in centimeters?"))
length_cut = int(input(" How long will you cut your slices in
centimeters?"))
width_cut = int(input(" How wide will you cut your slices in
centimeters?"))
#-------------------------------------------------------------
###Calculations
#Area's
surface_area = (length*width)
length_area = (length//length_cut)
width_area = (width//width_cut)
slice_area = (length_cut*width_cut)
#
slice_amount = round((length_area)*(width_area),0)
slice_total = (length_cut*width_cut*slice_amount)
slice_waste = (surface_area-slice_total)
slice_total_area = (slice_total*slice_area)
slice_waste_area = (slice_waste*slice_area)
slice_total_percentage = round((slice_total_area//surface_area),0)
#percentage calculation
slice_waste_percentage = round((slice_waste_area//surface_area),0)
#percentage calculation
total_pieces = (length_area*width_area)
edge_pieces = (2*length_area + 2*(width_area-2))
center_pieces = (total_pieces-edge_pieces)
#-------------------------------------------------------------
###Converting from Integers to String
l_c = str(length_cut)
w_c = str(width_cut)
su_a = str(surface_area)
sl_a = str(slice_amount)
s_t = str(slice_total)
s_t_p = str(slice_total_percentage)
s_w = str(slice_waste)
s_w_p = str(slice_waste_percentage)
e_p = str(edge_pieces)
c_p = str(center_pieces)
#-------------------------------------------------------------
###Outputs for program
print (" Your cake has a surface area of",su_a,"square
centimeters.")
print ("You can cut",sl_a,"total",l_c+"x"+w_c,"slices of
cake.")
print ("These slice dimensions can cut a total of",s_t,"square
centimeters, or",s_t_p+"%, of the cake.")
print ("These slice dimensions will waste",s_w,"square centimeters,
or",s_w_p+"%, of the cake.")
print ("There will be",e_p,"edge pieces, and",c_p,"center pieces of
cake.")
print ("Enjoy your",cake_type,"cake!")
Hi,
I've tested the code and it is working as expected, it is giving expected result, find the sample output below.
Suspecting that round() function causing the problem, or the division operation.
So took the input as float instead of string,
please find the working code below
#-----------------------------------------------------------------------------------------------
print ("Welcome to the Cake Slice Program!")
cake_type = str(input("What kind of cake did you make?"))
length = float(input(" How long is the cake in
centimeters?"))
width = float(input(" How wide is the cake in centimeters?"))
length_cut = float(input(" How long will you cut your slices in
centimeters?"))
width_cut = float(input(" How wide will you cut your slices in
centimeters?"))
#-------------------------------------------------------------
###Calculations
#Area's
surface_area = (length*width)
length_area = (length//length_cut)
width_area = (width//width_cut)
slice_area = (length_cut*width_cut)
#
slice_amount = round((length_area)*(width_area),0)
slice_total = (length_cut*width_cut*slice_amount)
slice_waste = (surface_area-slice_total)
slice_total_area = (slice_total*slice_area)
slice_waste_area = (slice_waste*slice_area)
slice_total_percentage = round((slice_total_area//surface_area),0)
#percentage calculation
slice_waste_percentage = round((slice_waste_area//surface_area),0)
#percentage calculation
total_pieces = (length_area*width_area)
edge_pieces = (2*length_area + 2*(width_area-2))
center_pieces = (total_pieces-edge_pieces)
#-------------------------------------------------------------
###Converting from Integers to String
l_c = str(int(length_cut))
w_c = str(int(width_cut))
su_a = str(int(surface_area))
sl_a = str(int(slice_amount))
s_t = str(int(slice_total))
s_t_p = str(slice_total_percentage)
s_w = str(slice_waste)
s_w_p = str(slice_waste_percentage)
e_p = str(edge_pieces)
c_p = str(center_pieces)
#-------------------------------------------------------------
###Outputs for program
print (" Your cake has a surface area of",su_a,"square
centimeters.")
print ("You can cut",sl_a,"total",l_c+"x"+w_c,"slices of
cake.")
print ("These slice dimensions can cut a total of",s_t,"square
centimeters, or",s_t_p+"%, of the cake.")
print ("These slice dimensions will waste",s_w,"square centimeters,
or",s_w_p+"%, of the cake.")
print ("There will be",e_p,"edge pieces, and",c_p,"center pieces of
cake.")
print ("Enjoy your",cake_type,"cake!")
Get Answers For Free
Most questions answered within 1 hours.