0

This is my code...it errors out on line 5 and I don't know why. Thanks for the help in advance.

The error is invalid syntax.

from math import pi

radius = int(raw_input(("What radius would you like?"))

def area_of_circle(radius):
       return (radius ** 2) * math.pi

area_of_circle(radius)
4

4 回答 4

1

代替

from math import pi

只需使用

import math

from math import pi加载数学模块,但不是将模块分配给 name math,而是在模块中查找pi并将其分配给 name pi。这可能很方便,但也可能导致可怕的初始化顺序问题。

于 2013-09-06T00:37:11.450 回答
1

我相信您在第 2 行缺少右括号。但是正如 Marius 所提到的,您实际上并不需要这些括号。

from math import pi

radius = int(raw_input("What radius would you like?")) # Parenthesis removed

def area_of_circle(radius):
       return (radius ** 2) * math.pi

area_of_circle(radius)

最重要的是,您还应该查看@user2357112 的建议并使用import mathand math.pi。更新后的代码将如下所示:

import math # Not import pi

radius = int(raw_input("What radius would you like?")) # Parenthesis removed

def area_of_circle(radius):
       return (radius ** 2) * math.pi

# Printing here for feedback
print area_of_circle(radius)
于 2013-09-06T00:41:05.760 回答
1

它工作正常

import math

radius = int(raw_input(("What radius would you like?")))

def area_of_circle(radius):
       return (radius ** 2) * math.pi

print area_of_circle(radius)

您缺少“)”右括号,导入的库应该是数学而不是 pi。

于 2015-05-13T09:34:59.673 回答
-1
import math
#parameter

radius = int(input("Enter the radius: "))

def AreaOfCircle(radius):
    return (radius**2)*math.pi

print(AreaOfCirle(radius))
于 2015-05-13T09:25:52.147 回答