Cost of water volume in cents
Trying to write a program to find the cost of the volume of water in the pool in cents Keep hanging around the volume and answering and can't figure out what I am doing wrong. Any help would be great. These are the equations I am using.
Volume in cubic feet = length * width * height
Cost = cost cubic feet * Volume in cubic feet
#Assignment 1, Python, Run 1
length = float(input("Please enter the length of the pool in feet:"))
width = float(input("Please input the width of the pool in feet:"))
height = float(input("Please input the height of the pool in feet:"))
cost = float(input("Please enter the cost of water in cents per cubic foot:"))
volume = [length*width*height]
answer = [cost*volume]
source to share
The problem is that you made your variable "volume" an array.
volume = [ something ] # This syntax says "volume is an array that contains something
You cannot multiply an array by a float and expect a reasonable answer.
answer = [ cost * volume ] # Here you are multiplying a float by an array
I think you mean
volume = length*width*height
answer = cost*volume
print("The volume is {0}, giving a total cost of {1}".format(volume, answer))
source to share
Two problems I see. First, you create a list using square brackets, they are not needed:
volume = length * width * height
answer = cost * volume
In fact, they don't work the way you expect. Multiplying the list by an integer gives the list expanded to the desired size:
> print(4 * [6,7])
[6, 7, 6, 7, 6, 7, 6, 7]
Or Python 2 variant:
> print 4 * [6,7]
[6, 7, 6, 7, 6, 7, 6, 7]
The float multiplication throws a runtime error just like you do.
Second, you need to actually display some output to the user, for example with (as above, for Python 2, leave the outer brackets):
print(answer)
or
print("Volume for %.2f x %.2f x %.2f pool is %.2f" % (length,width,height,volume))
print("Cost at %.2f per cubic foot is %.2f" % (cost,answer))
source to share