Javicobos
Javicobos

Reputation: 1495

Python: typeerrors, numbers that are not integers, and variable problems

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

Upvotes: 0

Views: 155

Answers (3)

Pavel Reznikov
Pavel Reznikov

Reputation: 3208

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

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142136

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)

Upvotes: 1

fanjojo
fanjojo

Reputation: 109

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

Upvotes: 0

Related Questions