모든 것을 시도했으며 코드가 매우 비슷해 보이지만 변수를 정의하면 이름 오류가 계속 발생합니다.Python 3 표면 영역과 구형 체적을 반환하는 클래스 작성
from math import *
class Spheres:
def __init__(self, radius):
self.radius = r
self.area = 0
self.volume = 0
def getRadius(self):
return self.radius
def surfaceArea(self):
self.area = 4 * 3.14 * (r*r)
return (self.area)
def getVolume(self):
self.volume = (4/3) * 3.14 * (r * r * r)
return (self.volume)
def main():
r = input("Enter the radius of the sphere: ")
s = Spheres(r)
print("The volume of the sphere is: ", s.surfaceArea())
print("The surface area of the sphere is: ", s.volume())
if __name__ == '__main__':
main()
내가 오류 메시지는 다음과 같습니다 모든 연구의 self.radius
원인 self.radius
로 변경 위의 기능에
Traceback (most recent call last):
File "/local/files/jfult001/CSC221/lab10/9ball.py", line 30, in <module> main()
File "/local/files/jfult001/CSC221/lab10/9ball.py", line 26, in main print("The volume of the sphere is: ", s.surfaceArea())
File "/local/files/jfult001/CSC221/lab10/9ball.py", line 15, in surfaceArea self.area = 4 * 3.14 * (r*r)
NameError: global name 'r' is not defined
은'NameError' 기능 :
나는 R = 플로트() 입력 ("반지름을 입력")를 작성합니다? 질문에 포함시켜 주시겠습니까? –
또한 코드 들여 쓰기를 확인하십시오. 위의 들여 쓰기가 실제 코드가 들여 쓰여진 모양을 정확하게 반영합니까? –
아니요 클래스의 함수는 모두 첫 번째 것과 같이 들여 쓰여지고 오류는 다음과 같습니다. – user2306249