# Python NameError: name 'area' is not defined

Asked 6 days ago, Updated 6 days ago, 1 views

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")
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, t, t, t)
height = getDistance(t, t, t, t)
return width * height

def calcTriangleArea(t):
a = getDistance(t, t, t, t)
b = getDistance(t, t, t, t)
c = getDistance(t, t, t, t)
s = (a + b + c) / 2
return math.sqrt(s * (s - a) * (s - b) * (s - c))

def calcCircleArea(t):
return math.pi * t * t

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

2022-09-20 16:23

L[i] is neither a triangle nor a square nor a circle.

2022-09-20 16:23

Popular Tags
python x 1174
android x 247
c x 235
java x 192
javascript x 106
mysql x 30
html x 27
node.js x 24
php x 21
list x 19