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?
2 Answers 2
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.
6 Comments
from __future__ import division.float(1)/float(12)As I understood, it is a problem in your python code. I modified the first part as follows
if (12 <= eggs <= 47):
dozen = eggs/ 12
dozenprice = dozen * 0.50
extra = eggs % 12.0
extraprice = extra*((1.0/12.0)*0.50)
total = dozenprice + extraprice
print "Your total is " + str(total)
Result is :
Please enter the amount of eggs you have : 18
Your total is 0.75
You No need to use extra = float(eggs) % 12 because egg value is an integer. Simplified the if() variables as if (12 <= eggs <= 47):
/normal division, although from the question not clear that's the issue.