我是编程新手。我只是在学习由Udacity主办的python课程。在这里,下面规定的代码不打印函数返回。
你们能帮帮我😟吗,提前谢谢!
def cylinder_volume (height, radius = 5):
pi = 3.14159
return height * pi * radius ** 2
print (cylinder_volume(10, 5))
你的程序有一些问题,我已经整理好了(我记得我也开始学习python)。您不应该在函数定义中将变量radius
设置为 5,而是在调用函数时输入它,无论如何,这是您的代码整理了一些注释,希望对您有所帮助
import math
def cylinder_volume (height, radius):
pi = math.pi # more accurate representation of Pi using pythons math library
volume = height * pi * radius ** 2 # Creates a variable with volume value
print(volume) # prints out the volume
return volume # returns the volume
cylinder_volume(1, 5) # using this uses only the print statement in the function
print("-----------------")
print(cylinder_volume(1, 5)) # using this prints the print statement in the function as well as the value returned