如何在Python中生成随机字符串的随机序列

时间:2018-05-05 11:52:22

标签: python-3.x

在Python中,我试图生成一个6个字符的随机字母和数字块。虽然我已经能够生成随机的单个字符和数字,但我无法使用random.shuffle()更改其序列,最终会使用abc123而不是a1b23c。有谁知道如何随机化这些字符的序列?

我的代码是使用Pycharm编写的,导入Tkinter以创建窗口

import string
import random
from tkinter import *

root = Tk()

A = random.choice(string.ascii_uppercase)
B = random.choice(string.ascii_uppercase)
C = random.choice(string.ascii_uppercase)
D = random.choice(string.digits)
E = random.choice(string.digits)
F = random.choice(string.digits)

x = (A, B, C, D, E, F)
# I need the sequence, or 'x', to be randomized for the Label to display
ScrambledText = Label(root, text=x)
ScrambledText.pack()

root.mainloop()

1 个答案:

答案 0 :(得分:-1)

您似乎没有尝试使用random.shuffle。大概是你在那个元组上尝试过,并且被告知元组不支持项目分配 - 转而使用列表[A, B, C, D, E, F]。 - @jonrsharpe

相关问题