0

just started playing with python came across a little problem simple example area of square with exception handling for wrong input works when integers are correctly input --but should I input and string or char-I get Traceback (most recent call last): File "ex3.py", line 29, in area = width * length TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

#!/usr/bin/python

def error():
        print "no parameter entered - please enter parameter"

def get_width():
        width = None
        try:
                width = int(raw_input("please enter width of the room in meters: "))
                return width
        except:
                error()
                get_width()

def get_length():
        length = None
        try:
                length = int(raw_input("please enter length of the room in meters: "))
                return length

        except:
                error()
                get_length()

print "\nExercise 3: Area of a Room"

width = get_width()
length = get_length()
area = width * length

print "The area of a room with a width of "+str(width)+" and a length of "+str(length)+" is "+str(area)+" squared meters\n"

any help/explanation would be great thanks

1 Answer 1

1

You make a recursive call, but the result isn't going anywhere. You need to return it. For example:

return get_width()
Sign up to request clarification or add additional context in comments.

1 Comment

AH! ..awesome thankyou! - it works , but only half understand, i use the function recursively yes, woudnt the return width handle the result of this call too?

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.