python - 不知道为什么这个功能不起作用?
问题描述
想要输入半径和返回区域和周长。但是,运行时,没有错误,但什么也没有发生。我错过了什么
def circle ( radius ):
pi = 3.141592
a = pi * radius **2
c = 2 * pi * radius
print (f'Area is {a} and the circumference is {c}')
解决方案
所以看起来您在理解如何调用该函数时遇到了问题。
看看这段代码,看看我如何将它分配为等于circle(77)?这是调用函数的一种方式。请注意,您的函数可以位于单独的文件中,您可以从中导入它们。如果它们在同一个文件中,则它们需要位于引用它们的任何代码之上。
def circle(radius):
pi = 3.141592
a = 2 * radius * pi
c = 2 * pi * radius
print (f'Area is '+str(a)+' and the circumference is '+str(c))
#this line invokes the function
this = circle(77)