[英]Draw specific letters and numbers in python
I made a function to generate a combination of letters and random numbers in python:我做了一个 function 在 python 中生成字母和随机数的组合:
from random import choice, random, sample
def generateKeyId(self):
keyId = sample(ascii_uppercase,4)+sample(digits,4)+sample(ascii_uppercase,1)
return ''.join(keyId)
this is one of the exits for example:这是出口之一,例如:
DCAT5087H
But what I really want is to generate an output with a combination of 1-200 and AJ Example:但我真正想要的是生成一个 output 结合 1-200 和 AJ 示例:
6A
32F
60H
in that order, number and string.按该顺序,数字和字符串。
You can do the following:您可以执行以下操作:
from random import randint, choice
from string import ascii_uppercase
letters = ascii_uppercase[:10] # A-J
def generateKeyId():
return str(randint(1, 200)) + choice(letters)
>>> generateKeyId()
'33D'
>>> generateKeyId()
'115D'
>>> generateKeyId()
'62B'
You can try this:你可以试试这个:
from random import randint
def generateKeyId():
letters = ["A","B","C","D","E","F","G","H","I","J"]
keyId = str(randint(0,200)) + letters[randint(0,9)]
return keyId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.