我已经尝试了所有方法,我的代码似乎非常接近,但是当我定义变量时,我不断收到名称错误。
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