私はすべてを試しましたが、コードは非常に近いようですが、変数を定義すると名前エラーが発生し続けます。
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()
私が得るエラーメッセージは次のとおりです。
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