Python Forum
I need help with floats and int
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need help with floats and int
#1
I am working on this for my intro to programming class. The cpcf is 1.2 the width is 20, the length is 30, and the depth is 10. Please help me and tell me what I am missing to make this work.

#this program figures the cost for filling a public pool
#Caleb
cpcf = float(input("Enter how much the city charges for a cubic foot of water"))
PoolWidth = float(input("Enter the width of the pool"))
PoolLength = float(input("enter the length of the pool"))
PoolDepth = float(input("Enter how deep the pool is"))
Volume = PoolWidth * PoolLength * PoolDepth
cost = cpcf * Volume
print("the cost of filling the pool is: " + cost)
Reply
#2
In what way is it not already correct?  What's your current output, your expected output, and what (if any) errors are you getting?
Reply
#3
In the future, please put your code in python tags, see the BBCode link in my signature below. Also, it helps a lot if you give us the full text of the error you are getting.

In this case, you are trying to add a float to a string on the last line, which you can't do. You need to convert cost to a string. The easiest way to do this is with str(cost)

If you have been taught the format method of strings, you should review that. It will give you more control over how the output is formatted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Aug-25-2017, 04:20 PM)ichabod801 Wrote: In the future, please put your code in python tags, see the BBCode link in my signature below. Also, it helps a lot if you give us the full text of the error you are getting.

In this case, you are trying to add a float to a string on the last line, which you can't do. You need to convert cost to a string. The easiest way to do this is with str(cost)

If you have been taught the format method of strings, you should review that. It will give you more control over how the output is formatted.

Thank you so much. We have not been taught much to be honest.
Reply
#5
Another solution to your problem is to replace the "+" in the last line to a ",". That way, you are not trying to add a string and a float together, but just printing them out seperately.

print("the cost of filling the pool is: ",cost)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Round off floats in a list nagymusic 10 4,680 Apr-02-2019, 12:43 AM
Last Post: nagymusic
  def functions and floats alwillia 3 4,383 May-26-2018, 07:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

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