2

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
mars
  • 49
  • 5
  • 6
    White space matters in Python, that's not syntactically valid. – jonrsharpe Nov 21 '16 at 22:28
  • 1
    @CliffBurton the pasted in code was formatted improperly. There is a legitimate misunderstanding of how Python2.7 interprets `/` normal division, although from the question not clear that's the issue. – TemporalWolf Nov 22 '16 at 00:08

2 Answers2

3

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.

Community
  • 1
  • 1
TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
  • 2
    To add onto this, you can get the behavior you expect by starting your program with `from __future__ import division`. – user3030010 Nov 21 '16 at 22:39
  • 1
    alternatively cast both numbers as a float. `float(1)/float(12)` – Albert Rothman Nov 21 '16 at 22:40
  • 1
    @user3030010 I've added that to the description. – TemporalWolf Nov 21 '16 at 22:41
  • 1
    @user3030010 i'm sorry, i'm immensely new to python programming so i'm a bit confused. how would I start my program with from_future_ import division? thank you so much for your help though! – mars Nov 21 '16 at 23:46
  • 2
    Just stick that line verbatim at the top of your program, like any other import statement. It takes the new division behavior (from Python 3) and lets you use it in Python 2. So if you were using Python 3, you wouldn't even have to do that. – user3030010 Nov 21 '16 at 23:48
  • @user3030010 Thank you so much!! – mars Nov 21 '16 at 23:55
0

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):

Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94