Python | NCSS Challenge - Cake Sale

/ 5 Aug 2013 /
FINALLY, I HAVE BEEN WAITING MY WHOLE LIFE. IT IS FINALLY TIME FOR THE CHALLENGE. OK, HERE'S THE FIRST QUESTION:

You are at a bake sale and want to choose your cake carefully based on value for money. All the cakes are the same height, but come in different sized square tins, marked by their side length.


Write a program to read in the side length and cost of two cakes and print out the cost of each cake per cm2 for how many cm2, and then say which cake to get, or to Get either!

Took me a little while, BUT I GOT IT IN THE END. HERE IS MY CODE:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
cake_len = float(input('Cake 1 side length (cm): '))
cake_cost = float(input('Cake 1 cost ($): '))
cake1 = cake_cost / cake_len**2

cake2_len = float(input('Cake 2 side length (cm): '))
cake2_cost = float(input('Cake 2 cost ($): '))
cake2 = cake2_cost / cake2_len**2

cm1 = round(cake_len**2)
cm2 = round(cake2_len**2)

print('Cake 1 costs ${:.2f} per cm2 for {} cm2'.format(cake1, cm1))
print('Cake 2 costs ${:.2f} per cm2 for {} cm2'.format(cake2, cm2))

if cake1 < cake2:
    print('Get cake 1!')
elif cake2 < cake1:
    print('Get cake 2!')
elif cake1 == cake2:
    print('Get either!')
 
Copyright © 2010 M(ath)+me, All rights reserved