Find the width of Python shapes

Asked 2 years ago, Updated 2 years ago, 17 views

I'm trying to create a program that lists the content in the text file to find the area of the shape that fits the shape and coordinates of the content, but I get an error saying that it's out of the list.

import math
L = []

import os
path = "C:/temp"
filelist = os.listdir(path)

f = open("MP09data.txt")
lines = f.readlines()
for line in lines:
    print(line, end = '')
f.close


def getDistance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

def calcRectangleArea(t):
    width = getDistance(t[2], t[1], t[0], t[1])
    height = getDistance(t[0], t[3], t[0], t[1])
    return width * height

def calcTriangleArea(t):
    a = getDistance(t[0], t[1], t[2], t[3])
    b = getDistance(t[2], t[3], t[4], t[5])
    c = getDistance(t[0], t[1], t[4], t[5])
    s = (a + b + c) / 2
    return math.sqrt(s * (s - a) * (s - b) * (s - c))

def calcCircleArea(t):
    return math.pi * t[2] * t[2]


for i in range(0, 10, 2):
    if L[i] == 'Square':
        area = calcRectangleArea()
    elif L[i] == 'Triangle':
        aerea = calcTriangleArea
    elif L[i] == 'Won':
        area = calcCircleArea

    print(L[i])
    print ("area: ", area)
Traceback (most recent call last):
  File "C:\Users\Desktop\Desktop\MP09\202110943 Ahn Se-ho's Practical Assignment #09.py", line 35, in <module>
    if L[i] == 'Square':
IndexError: list index out of range

What's the problem? How do I fix it?

python

2022-09-20 16:24

1 Answers

You are not putting anything in the empty list L defined at the top.

You'll have to do something append().


2022-09-20 16:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.