[Python] "NameError: name 'x' is not defined" 해결


[Python]

문제상황: 파이썬에서 작성한 코드에서 변수를 사용하기 전에 정의하지 않으면 NameError가 발생합니다. 실무에서 사용될 수 있는 코드 예시는 다음과 같습니다. def calculate_area(): area = width * height width = 10 height = 5 calculate_area()위 코드를 실행하면 다음과 같은 에러로그가 발생합니다. NameError: name 'width' is not defined해결방법: 에러가 수정된 코드와 수정된 부분에 대한 주석을 포함합니다. # 수정된 부분: 함수 인자로 width와 height를 전달합니다. def calculate_area(width, height): area = width * height return area ..


원문링크 : [Python] "NameError: name 'x' is not defined" 해결