简体   繁体   English

如何使用 .join 生成两个随机字符?

[英]How can I generate two random chars with .join?

I'm trying to make my program print a serial with a base and two randoms我试图让我的程序打印一个带有基数和两个随机数的序列

import random
import string

charList = "A", "B", "C", "D", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5"
"6", "7", "8", "9"
y27Base = ["U0330V", "U0330W", "U0330X", "U0330Y"]

whatSerial = input("What serial?").upper()

def y27(y27Base, charList):
    return y27Base + "".join([random.choice(charList) for x in range(2)]) + " -> Y27"
if whatSerial == "Y27F":
    for i in y27Base:
        for i2 in charList:
            print(y27(i, i2))

what I want is for the program to print y27Base with 2 randoms so for ex我想要的是让程序用 2 个随机数打印 y27Base 所以例如

U0330V9D U0330XZ7 U0330V9D U0330XZ7

and I'd want it to print as many as possible我希望它尽可能多地打印

Thanks alot in advance非常感谢提前

Your critical problem is that you pass single characters to the function:您的关键问题是您将单个字符传递给函数:

    for i2 in charList:
        print(y27(i, i2))

Thus, your function has only one character from which to choose, and you get the double letters that plague your output.因此,您的函数只有一个字符可供选择,并且您会得到困扰您的输出的双字母。

To fix this, simply pass in the character list from your main program:要解决此问题,只需从主程序中传入字符列表:

for i in y27Base:
    print(y27(i, charList))

Output:输出:

U0330VFG -> Y27
U0330WDC -> Y27
U0330XEM -> Y27
U0330YCI -> Y27

There is no limit to "as many times as possible": you can loop on this inner statement as much as you like. “尽可能多的次数”没有限制:您可以根据需要尽可能多地循环这个内部语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM