Python Forum
Bill calculator with different prices
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bill calculator with different prices
#1
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!
Reply
#2
You've been told before to use Python tags when posting code. See the BBCode link in my signature for instructions.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Is there anything else I need to do?
Reply
#6
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'>").
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Reply
#8
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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!
Reply
#10
delete line 2
replace line 3 with:
total_price = 0.0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with creating dynamic columns with for loops for stock prices PaDat 2 896 Feb-22-2023, 04:34 AM
Last Post: PaDat
  SMA (simple moving avg) Not receiving Data (stock prices). gdbengo 2 1,442 Jul-31-2022, 08:20 PM
Last Post: paulyan
  Different prices per hour kemper 1 2,019 Jan-29-2019, 11:06 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020