Python Forum
Python exercise - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Python exercise (/thread-12992.html)



Python exercise - janaraguz - Sep-22-2018

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


RE: Python exercise - ichabod801 - Sep-22-2018

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.


RE: Python exercise - ichabod801 - Sep-22-2018

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().