편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2021.05.18

    파이썬 NameError: name 'area' is not defined


    텍스트 파일의 내용을 리스트로 저장하여 리스트 값을 통한 도형 넓이 구하는 프로그램입니다.

    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] == '사각형':
            area = calcRectangleArea(L[i + 1])
        elif L[i] == '삼각형':
            area = calcTriangleArea(L[i + 1])
        elif L[i] == '원':
            area = calcCircleArea(L[i + 1])
    
        print(L[i])
        print("면적: ", area)
    

    이렇게 작성하면

    사각형
    Traceback (most recent call last):
      File "C:\Users\Desktop\Desktop\MP09\202110943 안세호 실습과제 #09.py", line 43, in <module>
        print("면적: ", area)
    NameError: name 'area' is not defined
    

    오류가 뜹니다.... 'area' 정의가 안된 이유를 모르겠습니다.

  • 프로필 알 수 없는 사용자님의 편집
    날짜2021.05.17

    파이썬 질문있어요


    텍스트 파일의 내용을 리스트로 저장하여 리스트 값을 통한 도형 넓이 구하는 프로그램입니다.

    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] == '사각형':
            area = calcRectangleArea(L[i + 1])
        elif L[i] == '삼각형':
            area = calcTriangleArea(L[i + 1])
        elif L[i] == '원':
            area = calcCircleArea(L[i + 1])
    
        print(L[i])
        print("면적: ", area)
    

    이렇게 작성하면

    사각형
    Traceback (most recent call last):
      File "C:\Users\Desktop\Desktop\MP09\202110943 안세호 실습과제 #09.py", line 43, in <module>
        print("면적: ", area)
    NameError: name 'area' is not defined
    

    오류가 뜹니다.... 'area' 정의가 안된 이유를 모르겠습니다.