3

Trying to write a program to find the cost of the volume of water in a pool in cents Keep getting hung up on volume and answer and can't figure out what I'm doing wrong. Any help would be great. These are the equations I'm using.

Volume in cubic feet = length * width * height
Cost = cost per 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]
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • I am not sure about the error you getting, I think you are not using correct multiplication function, please refer http://stackoverflow.com/questions/595374/whats-the-python-function-like-sum-but-for-multiplication – braintech Sep 15 '14 at 02:05

3 Answers3

2

The problem is that you have made your variable "volume" be an array.

volume = [ something ]    # This syntax says "volume is an array that contains something

You can't multiply an array by a float and expect to get a sensible 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))
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • So what am I doing wrong in this line? print "The cost of water to fill the pool is"(answer)" cents." – Charles Taylor Sep 15 '14 at 02:19
  • 1
    You can't just run a variable right up against a string like that. I showed in my edited answer one (very flexible) way of dealing with this. The other is simply print "the cost is", answer, "cents". See the commas to separate the items you want to print. – GreenAsJade Sep 15 '14 at 02:21
  • 1
    Also note, as other commenters are mentioning, that the format of how you use print differs between python 2.x and python 3.x, so we're all trying to guess which you have, to give you the best information :) – GreenAsJade Sep 15 '14 at 02:31
  • print "The cost is", answer, "cents" when I use this I get an invalid syntax. – Charles Taylor Sep 15 '14 at 02:33
  • 1
    Yes, see my comment about python 3. I think we can conclude you are using python3, which means you need to use parens: print("The cost is", answer, "cents") – GreenAsJade Sep 15 '14 at 02:34
  • Thanks for the help! I missed class due to a funeral! you saved my grade! – Charles Taylor Sep 15 '14 at 02:36
  • @GreenAsJade I've approved your edit to the OP's post as I agree it should be tagged Python3, but it didn't require the removal of the main python tag - that should have been left alone... (something to bear in mind for future edits) :) – Jon Clements Sep 15 '14 at 02:37
  • 1
    CharlesTaylor Welcome to Stack Overflow, I see this is your first question. Glad we could help. The customary thing to do when you're happy with the answer(s) is click the big tick on the one you accept, and upvote all the ones that contributed something along the way. (Thx @JonClements noted) – GreenAsJade Sep 15 '14 at 02:40
0

i think you meant to use parenthesis not brackets which in this context means a python list.

so your code:

volume = [length*width*height]

answer = [cost*volume]

should be:

volume = length * width * height

answer = cost * volume
Eduardo Briguenti Vieira
  • 4,351
  • 3
  • 37
  • 49
mms
  • 9
  • 2
0

Two problems as I see it. The first is that you're creating a list by using the square brackets, they're not needed:

volume = length * width * height
answer = cost * volume

In fact, they don't work as you'd expect. Multiplying a list by an integer gives you a list expanded to the desired size:

> print(4 * [6,7])
[6, 7, 6, 7, 6, 7, 6, 7]

Or the Python 2 variant:

> print 4 * [6,7]
[6, 7, 6, 7, 6, 7, 6, 7]

Multiplying a float, as you do, causes a run-time error.

Secondly, you need to actually display some output to the user, such as with (as above, for Python 2, leave off the outer parentheses):

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))
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • OP might be using Python 3 (since they do `input` instead of `raw_input`) – Michael0x2a Sep 15 '14 at 02:16
  • Another nitpick, sorry -- in Python 3, you'd want to do `print(...)` without the space between the `print` and the parens, since it's a function. Using `str.format` is also generally preferred over using the `%`-style string interpolation (though it looks like OP is only just learning about string concatenation and using variables, and so might not know what functions/methods are) – Michael0x2a Sep 15 '14 at 02:27
  • Actually, there's a few things in PEP8 that I don't follow, it is a _guide_ after all :-) Python3 works fine with the spaces. And I use the `printf`-style since that's what I'm used to, except in cases where I want to re-use arguments, such as `{1} {2} {1}`, which is pretty rare for me. But I'll make the `print` change since the vast majority of people probably do follow PEP8. – paxdiablo Sep 15 '14 at 02:40