0

Help with this code please :)

Fellow Programmers :) I am in need to get this code right and running. I need to compare 2 items and display which one is the most efficient one here is my code please help any way you can or point me in the right direction What am I doing wrong? import math def shape_volume(the_shape): if the_shape == 'round': the_radius = float(input("Please enter the sphere's radius: ")) shape_volume = (4 / 3) * math.pi * (the_radius ** 3) elif the_shape == 'square': the_height = float(input('Please enter the height of the Basket: ')) the_width = float(input('Please enter the width of the Basket: ')) shape_volume = the_height * the_width return shape_volume first_item = input('Enter the shape of Basket 1 (either square or round): ') the_price = input('Enter the price of Basket 1: ') the_size = input('Enter the size of Basket 1: ') first_volume = shape_volume(first_item) second_item = input('Enter the shape of Basket 2 (either square or round): ') the_price = input('Enter the price of Basket 2: ') the_size = input('Enter the size of Basket 2: ') second_volume = shape_volume(second_item) print('Basket ' ' is the most efficient: '+str(first_volume+second_volume))

15th Jan 2017, 7:43 AM
Tom
3 odpowiedzi
+ 2
@Tom wrote: "Now how can i make basket 1 or basket 2 when asking for size if both baskets are same shape. In the modified code it will ask twice for size I only need it to ask once for size each basket." Prepend the second line inputing 'the_size' by: if second_item != first_item : ... so it execute the second assignation/input only if the two shapes are differents ^^ Do you let me give you a tiny advise? You should rename the 'shape_volume' function or variable ( I would rename the variable: 'the_volume' ) to avoid confusing ;)
15th Jan 2017, 8:41 AM
visph
visph - avatar
0
Now how can i make basket 1 or basket 2 when asking for size if both baskets are same shape. In the modified code it will ask twice for size I only need it to ask once for size each basket. example: Enter the shape of Basket 1 (either square or round): round Enter the Price of Basket 1: 12 Enter the shape of Basket 2 (either square or round: round Enter the Price of Basket 2:
15th Jan 2017, 8:30 AM
Tom
0
Your calculation of volume for the round basket (sphere) is correct. For the square basket (cube), the volume should be calculated by multiplying the height, width and length. The good thing is that all those three values will be the same for a cube, and so you only need to input one value and calculate to the power of 3. I have modified your code to the reflect the above changes, and corrected the print statement, in the assumption that you need to find which basket is more efficient than the other based on the volume. Not sure whats the relevance of price/size there, and you didn't seem to use those values, so I commented those out. Feel free to add/modify/discard as you wish. Hope that helps. Here's the code: import math def shape_volume(the_shape): if the_shape == 'round': the_radius = float(input("Please enter the sphere's radius: ")) shape_volume = (4 / 3) * math.pi * (the_radius ** 3) elif the_shape == 'square': the_height = float(input('Please enter the height of the Basket: ')) #the_width = float(input('Please enter the width of the Basket: ')) shape_volume = the_height ** 3 return shape_volume first_item = input('Enter the shape of Basket 1 (either square or round): ') #the_price = input('Enter the price of Basket 1: ') #the_size = input('Enter the size of Basket 1: ') first_volume = shape_volume(first_item) print('Volume of Basket 1 is ' + str(first_volume)) second_item = input('Enter the shape of Basket 2 (either square or round): ') #the_price = input('Enter the price of Basket 2: ') #the_size = input('Enter the size of Basket 2: ') second_volume = shape_volume(second_item) print('Volume of Basket 2 is ' + str(second_volume)) if first_volume > second_volume: print('Basket 1 is more efficient than Basket 2') elif second_volume > first_volume: print('Basket 2 is more efficient than Basket 1') else: print('Basket 1 and Basket 2 are equally efficient')
18th Jan 2017, 10:19 PM
Alex