Python:print NameError

时间:2013-10-06 09:04:02

标签: python nameerror

import math 
r_out = int(input("Insert rings outside raadius: ")) 
r_in = int(input("Insert rings inner raadius: "))

# ring size calculating function
def areaOfRing(r_out, r_in):
    r_outs = (r_out**2)*math.pi
    r_ins = (r_in**2)*math.pi
    ring_size  = r_outs - r_ins
    return ring_size

ring_size = areaOfRing 
print('Ring size is = ',ring_size)

当你把2个数字放进去的时候,我试着打印出应该做的答案。在我运行之后,我得到了NameError。是。我确实尝试了ring_size = areaOfRing()ring_size = areaOfRing(r_out, r_in)给了我两个错误。

(注意,重新启动程序后,它开始使用@ Martijn的帮助。)

2 个答案:

答案 0 :(得分:0)

您没有正确调用您的功能,请执行以下操作:

ring_size = areaOfRing(r_out, r_in)

答案 1 :(得分:0)

调用函数时需要提供arguments

import math


# ring size calculating function
def areaOfRing(r_out, r_in):
    r_outs = (r_out**2)*math.pi
    r_ins = (r_in**2)*math.pi
    ring_size  = r_outs - r_ins
    return ring_size

r_out = int(input("Insert rings outside raadius: ")) 
r_in = int(input("Insert rings inner raadius: "))

ring_size = areaOfRing(r_out, r_in) 
print('Ring size is = ',ring_size)