【发布时间】:2021-12-31 05:55:48
【问题描述】:
所以我目前正在为我的 python 编码类做作业,我必须得到一个圆柱体的面积,我应该使用函数,所以它看起来更干净,但我不太确定如何准确从头开始做作业,我看过很多视频,但似乎无法真正理解函数,我的代码目前看起来像这样,但是每当我运行我的代码时,我都无法获得“def calc():”部分要运行,请问我可以指点一下吗?
def info():
r = float(input("What is the radius of the cylinder? "))
h = float(input("What is the height of the cylinder? "))
print("The cylinders area is", area)
def calc():
pi = 22/7
area = ((2*pi*r) * h) + ((pi*r**2)*2)
return pi, area
info()
【问题讨论】:
-
嗨,info() 中的最后一行应该是
print("The cylinder's area is ", str(calc(r, h)))。您还应该将def calc():更改为def calc(r, h):。 注意转换为 str! 而且您不应该返回pi。只返回area。这是你唯一需要的。