你能从列表中找到一个随机变量吗?

时间:2018-02-27 21:37:31

标签: python turtle-graphics

使用名为Turtle的图形引擎,我在特定点(括号中的前2个数字)创建了点,但是在那里创建了一个图片。我为每个人分配了一个变量。有没有办法可以随机生成以随机查找这些变量中的一个或多个?

 def numberpin():
    p1 = [pin(0,0,50)]
    p2 = [pin(50,50,50)]
    p3 = [pin(-50,50,50)]
    p4 = [pin(100,100,50)]
    p5 = [pin(-100,100,50)]
    p6 = [pin(150,150,50)]
    p7 = [pin(-150,150,50)]
    p8 = [pin(50,150,50)]
    p9 = [pin(-50,150,50)]
    p10 = [pin(0,100,50)]

1 个答案:

答案 0 :(得分:0)

您正在寻找类似的东西:

from random import choice

pin_dictionary = {
    'p1': pin(0, 0, 50),
    'p2': pin(50, 50, 50),
    'p3': pin(-50, 50, 50),
    'p4': pin(100, 100, 50),
    'p5': pin(-100, 100, 50),
    'p6': pin(150, 150, 50),
    'p7': pin(-150, 150, 50),
    'p8': pin(50, 150, 50),
    'p9': pin(-50, 150, 50),
    'p10': pin(0, 100, 50),
}

def random_pin():
    return pin_dictionary[choice(list(pin_dictionary))]