mars - 1 year ago 225
Python Question

# Programming Decision Structures

A wholesale egg company bases their prices on the number of eggs purchased:

0 up to but not including 4 dozen \$0.50 per dozen 4 up to but not including 6 dozen \$0.45 per dozen 6 up to but not including 11 dozen \$0.40 per dozen 11 or more dozen \$0.35 per dozen Extra Eggs are priced at 1/12 the per dozen price

Create a program that prompts the user for the number of eggs, and then calculates bill. The application output should look similar to: Enter the number of eggs purchased: 18 The bill is equal to: \$0.75

This is the question and here is my code:

``````eggs = int(raw_input("Please enter the amount of eggs you have."))

if (eggs >=12 and eggs <=47):
dozen = int(eggs) // 12
dozenprice = float(dozen) * 0.50
extra = float(eggs) % 12
extraprice = float(extra)*((1/12)*0.50)
total = float(dozenprice) + float(extraprice)
print "Your total is " + str(total)

if (eggs >=48 and eggs<=71):
dozen = int(eggs) // 12
dozenprice = float(dozen) * 0.45
extra = float(eggs) % 12
extraprice = float(extra)*((1/12)*0.45)
total = float(dozenprice) + int(extraprice)
print "Your total is " + str(total)

if (eggs >=72 and eggs <=131):
dozen = int(eggs) // 12
dozenprice = float(dozen) * 0.40
extra = float(eggs) % 12
extraprice = float(extra)*((1/12)*0.40)
total = float(dozenprice) + int(extraprice)
print "Your total is " + str(total)

if (eggs >=132):
dozen = int(eggs) // 12
dozenprice = float(dozen) * 0.35
extra = float(eggs) % 12
extraprice = float(extra)*((1/12)*0.35)
total = float(dozenprice) + int(extraprice)
print "Your total is " + str(total)
``````

Why isn't the price for the extra eggs appearing?

`1/12` in python2.7 is `0`, because they are integers and using integer division you get 0. Then you get `0 * price`...
`1/12.` will produce `0.083`, as the `12.` forces it to be a float. Alternatively, you can get Python3 behavior like so:
``````from __future__ import division
Without that, in Python2.7, `/ & // are equivalent`.