Python Forum

Full Version: Python exercise
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I was trying to solve an exercise where I needed to calculate the area of the box where I should input the coordinates of the specific box (Box1 or Box2) and then calculate the area.
This is my code so far:

import numpy as np


def boxArea(boxCorners, area):
    x = np.array([boxCorners])
     
    if area == "Box1":
        x = np.array([boxCorners])
        A = ((x[1]-x[0])*(x[3]-x[2]))
        
    elif area == "Box2":
        
        A = ((x[1]-x[0])*(x[3]-x[2]))
        
        return A
The problem is that I constantly get this error message: "IndexError: index 1 is out of bounds for axis 0 with size 1". I really do not understand what is the problem in this code. Hope that someone can help me. Smile
Please use Python tags when posting code. I put them in for you this time. See the BBCode link in my signature below for instructions.
I don't think you want the brackets around boxCorners, but it's unclear what input you are expecting. Clarifying that would be helpful.

Note that the return statement should be unindented one level. Otherwise it will only return a value for 'Box2'. Also, I don't see any different between the two calculations.I don't even see the need for np.array().