首页 > 解决方案 > 如何在python 3中获取圆柱体体积

问题描述

我是编程新手。我只是在学习由 Udacity 主办的关于 python 的课程。这里,下面规定的代码不打印函数返回。

请各位帮帮我提前谢谢!

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

print (cylinder_volume(10, 5))

标签: python

解决方案


我整理的程序有一些问题(我记得我也开始学习 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

推荐阅读