python - 如何在python 3中获取圆柱体体积
问题描述
我是编程新手。我只是在学习由 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
推荐阅读
- python - Pandas 读取 CSV 和 Seaborn - ValueError:无法解释输入
- rest - 休息服务 - Tomcat 能够启动,主页工作正常但不能做任何 GET
- generics - 如何为 Rust 泛型函数中绑定的 ?Sized 多类型赋予可重用名称
- android - 在 Android 中获取屏幕时间统计信息的 API
- mongodb - MongoDB 通过聚合查找的连接结果排序
- terraform - Terraform 代码不会创建 EC2 实例
- python - 轴上的短垂直线(matplotlib)
- c# - 从项目目录中读取文本文件内容并将参数传递给其文本
- apache-spark - WARN BlockManagerMasterEndpoint:没有更多副本可用于 rdd
- python-3.x - 如何从 python 脚本运行终端命令?