python - 获取一个参数

标签 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

最佳答案

你不能;他们已被重新分配到一个新号码。也就是说,当获取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/

相关文章:

python - VS code 不会检查我的代码,但内部构建会检查我的代码

python - Python 中的电路门

java - 在 Java 中更快地重新制定方程式

c++ - 为什么 C++ 支持带实现的纯虚函数?

python - 在 Python 中使用 '@patch.object' 和 'with patch.object' 有什么区别?

python - heapq 库中函数的时间复杂度是多少

python - 是否可以生成Jupyter笔记本的可执行文件(.exe)?

c - 将结构体值从 function() 返回到 main()

python - 使os.walk/os.stat忽略权限被拒绝?

python - 对工作日文本进行排序