获取参数

时间:2015-05-27 11:37:54

标签: python function python-2.7 python-3.x

我有一个绘制圆圈并随机回答的功能 (这里的答案是1-100之间的数字)

def circle(x,y,ans):
    #RandomAnswer
    global RaAns
    RaAns = random.randint(1, 100)
    tr.up()
    #center of circle is: (x,y)
    tr.goto(x,y-40)
    tr.down()
    tr.fill(1)
    tr.color(0.2,0.2,0.2)
    tr.circle(40)
    tr.color(0.2,0.6,0.6)
    tr.fill(0)
    tr.up()
    tr.goto(x-15,y-15)
    tr.down()
    tr.color(0.2,0.2,0.2)
    tr.write(RaAns,font=("Ariel",20))

我得到了那个:

C1 = circle(150,245,RaAns)
C2 = circle(245,150,RaAns)

我的问题是如何选择C1 RaAns以及如何选择C2 RaAns

1 个答案:

答案 0 :(得分:1)

你不能;他们已被重新分配到一个新号码。也就是说,当获得C2时,RaAns已被重新分配。你应该这样做的方法是返回RaAns或完全放弃它并使用ans参数。

def circle(x, y, ans=None):
    if ans is None:
        ans = random.randint(1, 100)
    ...
    tr.write(ans, font=("Arial", 20))
    return ans

C = [None] * 3
C[1] = circle(150, 245)
C[2] = circle(245, 150)

# C is now [None, *C1's random number*, *C2's random number*]

如果您必须返回其他内容,请预先输入随机数。

def circle(x, y, ans):
    ...
    tr.write(ans, font=("Arial", 20))
    return something

C = [{}, {"rand": random.randint(1, 100)}, {"rand": random.randint(1, 100)}]
C[1]["circle"] = circle(150, 245, C[1]["rand"])
C[2]["circle"] = circle(245, 150, C[2]["rand"])

# C is now [None,
#           {"rand": *C1's random number*, "circle": *What circle returned*},
#           {"rand": *C2's random number*, "circle": *What circle returned*}]