简体   繁体   English

如何在python中使用某些规则生成随机数或字母

[英]How do I generate a ranndom number or letter with certain rules in python

I'm trying to generate a random number but with certain rules. 我正在尝试生成一个具有一定规则的随机数。 The first 11 digits to be 00000001008 so I just made it a string and printed it to the screen then after the string, the next number is a random digit or a letter, so I'm using the random and string module to generate a random digit or letter. 前11个数字是00000001008所以我只是将其制成字符串并打印到屏幕上,然后在字符串之后,下一个数字是随机数字或字母,因此我正在使用random and string模块生成随机数数字或字母。 After the random generated number I want a set of three 1's so I just printed them as a string like I did with the first eleven values. 在随机生成的数字之后,我想要一组三个1,所以我像对前11个值一样将它们打印为字符串。 But here's where I'm getting stuck, after the set of three 1's I want to generate another random number or letter and I can't figure it out, I tried the same line of code as the random variable, with a new variable name and then I tried to concatenate it with the new variable in the print statement and it gives me an error. 但是在这里,我很困惑,在三个三个1的集合之后,我想生成另一个随机数或字母,但我无法弄清楚,我尝试了与随机变量相同的代码行,并使用了新的变量名然后我尝试将其与print语句中的新变量连接起来,这给了我一个错误。 How can I generate another random number after the three 1's? 如何在三个1之后生成另一个随机数?

import random 
import string

    random = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(1)])

    print ("00000001008")+ random + ("111")  

Output 产量

00000001008U111

Desired Output 期望的输出

00000001008(random)111(random)1(random)

Error 错误

import random 
import string

random = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(1)])
random1 = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(1)])
print ("00000001008")+ random + ("111") + random1

random1 = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(1)]) AttributeError: 'str' object has no attribute 'choice' random1 =''.join([xrange(1)中n的(random.choice(string.ascii_letters + string.digits)为n)])AttributeError:'str'对象没有属性'choice'

you can simply use string formatting and 3 random values 您可以简单地使用字符串格式和3个随机值

import random
import string

def gen():
  return random.choice(string.ascii_letters + string.digits)

print('00000001008%s111%s1%s' % (gen(), gen(), gen()))

and the only problem why your code is not working is that you named your variable random , which made module random impossible to access, thus error coming from your random.choice call which now refers to a string . 代码无法正常工作的唯一问题是,您将变量命名为random ,这使得模块无法访问random ,因此错误来自您的random.choice调用,该调用现在引用了string Rename your variables to "random_string" and "random_string1" and everything will be just fine. 将变量重命名为“ random_string”和“ random_string1”,一切都会好起来。

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

相关问题 如何计算 python 中的字母对数? - How do I count up the number of letter pairs in python? 如何根据文本而不是字母编号对 python 中的字符串进行切片? - How do I slice a string in python based on the text, not the letter number? 如何在 Python 数据框中的特定模式之前提取某个字母 n#s? - How do I extract a certain letter n#s before a specific pattern in a data frame in Python? 如何循环python命令一定次数? - How do I loop python commands for a certain number of times? 如何检查用户给定输入的字符串是否等于 Python 中的某个字母/单词 - How do I check if a string with a user given input is equal to a certain letter/ word in Python 如何在 python 3 中向后 state 代码并用某个字符替换字母? - How do I state a code backwards and replace a letter with a certain character in python 3? 如何在python中生成但排除某个数字整数中的某个数字 - How to generate but exclude a certain number in a certain digit integer in python 如何编写一个二维随机数数组,使其遵循 python 中的特定规则 - How do I program a 2D array of random numbers so it follows a certain rules in python 如何对以某个字母开头的单词进行二进制搜索? - How do I run a binary search for words that start with a certain letter? 我如何检查Python中某个字母的字符串? - How would I check a string for a certain letter in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM