如何在python中的变量旁边分配一个数字?

时间:2016-04-12 08:15:39

标签: python python-3.x

我试图制作x1,x2,x3等',但我不知道怎么做...... 我试着制作这段代码:

x1=random.choice(string.letters)
x2=random.choice(string.letters)
x3=random.choice(string.letters)
x4=random.choice(string.letters)
x5=random.choice(string.letters)
x6=random.choice(string.letters)
x7=random.choice(string.letters)
x8=random.choice(string.letters)
x9=random.choice(string.letters)
x10=random.choice(string.letters)

但当我尝试将其作为while循环编写时:

i=1 #Counter

#Letter Creation
while i<=10:
    x(i) = random.choice(letterschoice)

但是有错误。如何修复x(i)并将其设为x1,x2,x3等?

1 个答案:

答案 0 :(得分:2)

使用列表会更容易,而不是使用十个不同的变量:

[]

然后,您可以使用x运算符访问x[0]的特定元素。请注意,它使用从零开始的索引(即第一个元素是x[1],第二个元素是module.exports =等。)

相关问题