我有一个可以画圆圈并随机给出答案的函数
(这里的答案是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
?
最佳答案
你不能;他们已被重新分配到一个新号码。也就是说,当获取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*}]
关于python - 获取一个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30481341/