变量未定义.我该如何定义它?

-2 python

我有以下代码:

p=3.14159

#Function to circleArea
def circleArea(A):
    A=r*r*p
    radius=r
    return radius

#Main Program
r=int(input("Enter radius: "))
print("The area of the circle is:" )
print(circleArea(A))
Run Code Online (Sandbox Code Playgroud)

为什么下面的行会产生这个错误?

print(circleArea(A))
Run Code Online (Sandbox Code Playgroud)

NameError:未定义名称"A"

ale*_*cxe 6

看来你想r*r*pcircleArea()函数返回并r作为参数传递.此外,您可能希望使用math.pi而不是硬编码它的值:

import math

#Function to circleArea
def circleArea(r):
    return r * r * math.pi

#Main Program
r=int(input("Enter radius: "))
print("The area of the circle is:" )
print(circleArea(r))
Run Code Online (Sandbox Code Playgroud)

DEMO:

Enter radius: 10
The area of the circle is:
314.159265359
Run Code Online (Sandbox Code Playgroud)