Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python exercise
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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().
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Exercise Vasanth0910 1 1,860 May-15-2020, 04:41 PM
Last Post: pyzyx3qwerty
  Python exercise HY2000 2 3,158 Nov-07-2019, 08:01 AM
Last Post: HY2000
  Simple exercise - how to write in python tomtom55 6 4,915 Sep-28-2017, 07:18 PM
Last Post: nilamo
  Python Exercise: Generate a two-dimensional array smbx33 4 10,689 Apr-22-2017, 11:51 PM
Last Post: smbx33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020