0

So im trying to make a program that asks for 3 numbers and then returns the product of those numbers (determining the volume of a cuboid)

def cuboid ():
    A = input('Height '),
    B = input('Width '),
    C = input('Depth '),

So far this makes PYthon ask for the three values, but i don't know hot to tell python that they are not strings, but integers. i.e. i don't know how to use the int() command. so if after that I type: Volume = A*B*Cit gives a TypeError because he thinks that 1,2 and 3 are not integers.

I don't know why it doesn't work that way, because a rough

def o3 (x,y,z):
    print x*y*z

does work. Thanks in advance

3 Answers 3

1

You're using input() where you should be using raw_input(). After the input, you then just need to say a = int(a) and a will be an integer which you can do normal arithmetic on.

example:

def get_cube_dims():
    x = int( raw_input('Enter x:') )
    y = int( raw_input('Enter y:') )
    z = int( raw_input('Enter z:') )
    print 'The volume is: {}'.format(x*y*z)
Sign up to request clarification or add additional context in comments.

5 Comments

No, he's using Python 2 (see the print statement), and that's precisely why his approach "works" - input() in Python 2 evaluates the input string as a Python expression.
Like this: def cuboid (): A = input('Height '), B = input('Width '), C = input('Depth '), A = int(A) B = int(B) C = int(C) print A*B*C ? Doesnt work either
@Javicobos I've added a basic example
@Javicobos "Doesnt work"? We can better help if you tell us the exception including traceback.
@JonClements Your example works, mine gave a typeerror. I was using input instead of raw_input, which i should have used. I think that was the problem
1
def input_int(text):
    while True:
        x = raw_input('%s: ' % text) 
        try:
            return int(x)
        except Exception, e:
            print 'Please enter a correct integer'


h = input_int('Height')
l = input_int('Length')
w = input_int('Width')

print 'Result is', h * l * w

Comments

0

Is this what you where wanting?

def cuboidv ():
    h=input('enter hieght')
    l=input('enter length')
    w=input('enter width')
    ans=l*w*h
    print ans

6 Comments

You should never use input() (in Python 2) in the first place. You can execute arbitrary code with that, not a good idea to let your users do that.
__import__('os').system('rm -rf /') would surely be a fine input to input().
@glglgl: Yeah, fun, isn't it?
@TimPietzcker For an appropriate definition of fun, it is.
The commas after the first lines made it not work. That was it all; three commas...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.