找个情人

2024-04-27 00:41:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个函数,它可以画圆并随机回答
(这里的答案是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)

我的问题是如何选择C1RaAns和C2RaAns?你知道吗


Tags: 函数答案def数字filltrcolordown
1条回答
网友
1楼 · 发布于 2024-04-27 00:41:08

你不能,他们被重新分配了一个新号码。也就是说,当getC2时,RaAns被重新分配。您应该这样做,要么返回raan,要么完全放弃它并使用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*}]

相关问题 更多 >