A program that saves the contents of a text file as a list to obtain the width of the shape through the list value.
import math
L = []
import os
path = "C:/temp"
filelist = os.listdir(path)
f = open("MP09data.txt")
lines = f.readlines()
for line in lines:
L.append(line)
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(L[i + 1])
elif L[i] == 'Triangle':
area = calcTriangleArea(L[i + 1])
elif L[i] == 'Won':
area = calcCircleArea(L[i + 1])
print(L[i])
print ("area: ", area)
If you write it like this
Square
Traceback (most recent call last):
File "C:\Users\Desktop\Desktop\MP09\202110943 Ahn Se-ho's Practical Assignment #09.py", line 43, in <module>
print ("area: ", area)
NameError: name 'area' is not defined
An error appears.... I don't know why "area" isn't defined.
python
L[i] is neither a triangle nor a square nor a circle.
© 2024 OneMinuteCode. All rights reserved.