Python Forum
Bill calculator with different prices - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Bill calculator with different prices (/thread-12824.html)

Pages: 1 2


Bill calculator with different prices - JHPythonLearner - Sep-14-2018

Hello.

I'm trying to write a program that calculates the total price of photocopies made at a photocpy business.
Prices are as follows:
The first ten are at 10 cents
The next 20 are at 9 cents
The rest are at 8 cents.

This is what I have done:
------------------------------------------------------
n = int(input("Number of copies: "))
Number of copies: 50
total_price = float
if (n <= 10):
total_price = n * .10
elif (n <= 30):
total_price = 10 * .10 + (n - 10) * .09
else:
total_price = 10 * .10 + 20 * .09 + (n - 30) * .08
print("The total price is", total_price)
------------------------------------------------------
I'm getting the following:

The total price is <class 'float'>

Would you please help?

Thank you!


RE: Bill calculator with different prices - ichabod801 - Sep-14-2018

You've been told before to use Python tags when posting code. See the BBCode link in my signature for instructions.


RE: Bill calculator with different prices - JHPythonLearner - Sep-14-2018

n = int(input("Number of copies: "))
Number of copies: 50
total_price = float

if (n <= 10):
    total_price = n * .10
elif (n <= 30):
    total_price = 10 * .10 + (n - 10) * .09
else:
    total_price = 10 * .10 + 20 * .09 + (n - 30) * .08
print("The total price is", total_price)

Here it is. Did I do it right?


RE: Bill calculator with different prices - ichabod801 - Sep-14-2018

total_price = float is your problem. You are setting it to the type float, not an instance of a float (like 8.01). However, I'm not sure how you would get that. Any invalid input should be caught by the else statement, and total_price should be assigned a new value that is actually a float. What input are you giving it.

Note that even if you fix that, your calculation will be wrong. Lines 7 and 10 are incorrect, and will result in incorrect values.


RE: Bill calculator with different prices - JHPythonLearner - Sep-14-2018

Is there anything else I need to do?


RE: Bill calculator with different prices - ichabod801 - Sep-14-2018

I'm not sure. I can see where the problem comes from, but not how it survives until the end. Please tell me what input you are using to get that output ("The total price is <class 'float'>").


RE: Bill calculator with different prices - JHPythonLearner - Sep-14-2018

Thank you very much for your answer.
I'm assuming that I made 50 photocopies (is this the input you are referring to?)
Regarding lines 7 and 8, here is the logic. Let's say I make 29 copies (more than 10 but less than 30) In this case I would be charged 10 cents for each of the first 10 photocopies and 9 cents for each of the next 19 copies.

How do I fix the total_price variable? How do I define a variable whose value is contingent upon that of another variable (n in this case)?

The input I am using is the code on lines 1, plus the number 50 I entered when "Number of copies" appeared on line 2, plus the code on lines 3 to 11

Regarding lines 9 and 10, here is the logic. Let's say I made 50 copies (as I am assuming). I would be charged 10 cents for each of the first 10 copies, 9 cents for each of copies 11 to 30, and 8 cents for each of copies 31 to 50.


RE: Bill calculator with different prices - ichabod801 - Sep-14-2018

I just ran it with 50, and got 4.4, not <type float>. If you are typing in 50, I don't see how you are getting that answer.

For the logic errors, I misread the problem, so those lines are correct.


RE: Bill calculator with different prices - JHPythonLearner - Sep-14-2018

Awesome! Glad to know my algo is correct. For some reason I still get "The total price is <class 'float'>". But what's most important is the structure of my algo. Thank you very much for all your help!


RE: Bill calculator with different prices - Larz60+ - Sep-14-2018

delete line 2
replace line 3 with:
total_price = 0.0