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
124k29 gold badges181 silver badges319 bronze badges
asked Nov 21, 2016 at 22:28
2
  • 6
    White space matters in Python, that's not syntactically valid. Commented Nov 21, 2016 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. Commented Nov 22, 2016 at 0:08

2 Answers 2

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.

answered Nov 21, 2016 at 22:35
Sign up to request clarification or add additional context in comments.

6 Comments

To add onto this, you can get the behavior you expect by starting your program with from __future__ import division.
alternatively cast both numbers as a float. float(1)/float(12)
@user3030010 I've added that to the description.
@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!
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.
|
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):

answered Nov 22, 2016 at 1:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.