在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()
答案 0 :(得分:-1)
您似乎没有尝试使用random.shuffle
。大概是你在那个元组上尝试过,并且被告知元组不支持项目分配 - 转而使用列表[A, B, C, D, E, F]
。
- @jonrsharpe