这是我的Python代码:
def pyramid_volume(base_length, base_width, pyramid_height):
base_area = base_length * base_width
pyramid_volume = base_area * pyramid_height * 1/3
return
print('Volume for 4.5, 2.1, 3.0 is:', pyramid_volume(4.5, 2.1, 3.0))
打印Volume for 4.5, 2.1, 3.0 is: None
有人可以帮助我吗?
答案 0 :(得分:3)
在python中,你必须指定要返回的内容。像这样:
def pyramid_volume(base_length, base_width,pyramid_height):
base_area=base_length*base_width
pyramid_volume= base_area*pyramid_height*1/3
return pyramid_volume
print('Volume for 4.5, 2.1, 3.0 is: ', pyramid_volume(4.5, 2.1, 3.0))
答案 1 :(得分:0)
Python中的每个函数都返回一些对象。如果未明确指定任何对象,则返回None
(等效于null
的Python)。通过撰写return [nothing_here]
,您未指定返回的内容,因此会返回默认值(None
)。
作为解决方案,明确指定应返回的内容:
def pyramid_volume(base_length, base_width, pyramid_height):
base_area = base_length * base_width
pyramid_volume = base_area * pyramid_height * 1/3
return pyramid_volume
print('Volume for 4.5, 2.1, 3.0 is:', pyramid_volume(4.5, 2.1, 3.0)) # should print value