如何在python 3中获取圆柱体体积

时间:2018-12-22 17:16:24

标签: python

我是编程新手。我只是在跟随由Udacity托管的python课程。在此,以下规定的代码不会打印功能返回。

请大家帮我,谢谢!

def cylinder_volume (height, radius = 5):
pi = 3.14159
return height * pi * radius ** 2

print (cylinder_volume(10, 5))

1 个答案:

答案 0 :(得分:0)

我整理了一些程序错误(我还记得也开始学习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