Search code examples
pythonpython-2.7functionraw-input

keep getting an error 'function @ 0xb6ff9924 (or something similar)'


I am attempting to create a little program that will calculate the area of a shape. When ever I put in the name of the shape it prints out "function at 0xetc". I am having a heck of a time figure out where the issue is. I know that my area function is probably messed up but I can't get there to debug it. I initial thought that I had defined functions with the same names as the shapes, it was calling them from the input but I change the names and I get the same message. here is the code:

#geometry program

#shapes

def squar(x, y):
    result = x * y
    return result

def rectangl(x, y):
    result = x * y
    return result

def circl(r):
    result = 3.14 * (r^2)
    return result

def triangl(h, b):
    result = (h^b * b)/2
    return result

s = raw_input("Shapes are square, rectangle, circle or triangle. Pick a shape and I will calculate its area. ")



#Calculating the area

def area(s):
   if str(s) == "square":
        x = int(raw_input("Height? "))        
        y = int(raw_input("Width?" ))
        squar(x, y)
        return squar
    elif str(s) == "rectangle":
        x = int(raw_input("Height? "))        
        y = int(raw_input("Width?" ))
        rectangl(x,y)
        return rectrangl
    elif str(s) == "circle":
        r = int(raw_input("Radius?" ))
        circl(r)
        return circl
    elif str(s) == "triangle":
        h = int(raw_input("Height?" ))
        b = int(raw_input("Base Width?" ))
        triangl(h, b)
        return triangl

    else:
        print "I don't know that one"


print area

thanks ps. If the indenting looks a bit off by a space or two it is because of the formatting here. they aren't throwing up any error when I run the program.


Solution

  • You have in total 6 errors that you need to fix:

    • In area() class, remove all the 4 return instructions you have.
    • As the 4 methods you are calling in area() return values, you need to display their results by: print method_name(arguments)
    • Finally, you will have to correctly instantiate the class area()

    Here is how to fix the errors mentioned above:

    # Shapes
    def squar(x, y):
        result = x * y
        return result
    
    def rectangl(x, y):
        result = x * y
        return result
    
    def circl(r):
        result = 3.14 * (r^2)
        return result
    
    def triangl(h, b):
        result = (h^b * b)/2
        return result
    
    s = raw_input("Shapes are square, rectangle, circle or triangle. Pick a shape and I will calculate its area. ")
    
    # Calculating the area
    def area(s):
        if str(s) == "square":
            x = int(raw_input("Height? "))        
            y = int(raw_input("Width?" ))
            print squar(x, y)
        elif str(s) == "rectangle":
            x = int(raw_input("Height? "))        
            y = int(raw_input("Width?" ))
            print rectangl(x,y)
        elif str(s) == "circle":
            r = int(raw_input("Radius?" ))
            print circl(r)
        elif str(s) == "triangle":
            h = int(raw_input("Height?" ))
            b = int(raw_input("Base Width?" ))
            print triangl(h, b)
        else:
            print "I don't know that one"
    
    area(s)