Cthulhu
Cthulhu

Reputation: 21

I need to know how to get my function to return floats. I'm confused as to where I put the float tag

I am currently working through a python tutorial that has asked me to create a random function and run it 10 different ways. I'm stuck on how to actually get it to use floats. I Assume I should post the entire thing and just point out where I'm trying to get float to work

def volume (length, width, height):
    print "the object is %d cm long, %d cm wide, and %d cm high" % (length, width, height),
    total_volume = float(length * width * height)
    print "The total volumeis %d cm3" % total_volume


print "Direct input:"
volume (10, 20, 30)

print "direct input variables:"
length = (10)
width = (20)
height = (30)
volume (length, width, height)

print "direct input variables and math"
volume (length + 10, width +20, height +30)

print "direct input math"
volume (10 + 10, 20 +20, 30 + 30)


print "user input with int(raw_input)"
length2 = int(raw_input("what is the length? "))
width2 = int(raw_input("what is the width? "))
height2 = int(raw_input("what is the height? "))
volume (length2, width2, height2)

#here is the first problem
print "user input with float(raw_input)"
length3 = float(raw_input("what is the length? "))
width3 = float (raw_input("what is the width? "))
height3 = float (raw_input("what is the height? "))
volume (length3, width3, height3)

#Doesn't work here either`
print "float(raw_input) + variables"
print "the base oject size is 10 * 10 * 10"
print "why is this important? IT ISN'T!!!!!"
print "However, eventually I will make one that calculates the increase in volume"
length4 = length + float(raw_input("How much length are you adding? "))
width4 = width + float(raw_input("How much width are you adding? "))
height4 = height + float(raw_input("How much height are you adding? "))
volume (length4, width4, height4)

These two parts simply refuse to return a float. Here is what I tried so far.

I've attempted to add float when calling on the function variable, as follows

volume float(length4, width4, height4)

I tried to add float to the actual definition part of the function as follows

def volume float(length, width, height):

as you can see, I have float placed in the actually math part of the function, with no effect.

It must be possible to make this work. I am hoping someone more knowledgeable can point the way, and I am out of ideas

Upvotes: 1

Views: 69

Answers (2)

ImNotBot
ImNotBot

Reputation: 51

Use %f instead of %d when you want float instead of integer.

Also, you can format your strings more with "%0.2f" where 2 is how many decimal places you would like.

>>> x = 1.2342345
>>> print "%0.2f" % x
1.23

Upvotes: 1

Mureinik
Mureinik

Reputation: 311998

There's nothing wrong with your math, you're just printing the result as an integer by using %d. If you use %f instead, you should see the correct result:

print "The total volume is %f cm3" % total_volume
# Here ---------------------^

Upvotes: 1

Related Questions