[英]Can someone please help me with my random code generator?
So I'm trying to make a random code generator.所以我正在尝试制作一个随机代码生成器。 With 16 code places like this [x1gG4...].
有 16 个这样的代码位置 [x1gG4 ...]。 But if I run the program it gives me an error.
但是如果我运行该程序,它会给我一个错误。 Can someone help me?
有人能帮我吗?
#imports
import random
import time
#random string
str_var = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
#-----------Code
print "test"
print random.choice(str_var)
#just temporary
time.sleep(999)
您需要从str_var
替换绘制 16 次:
"".join([random.choice(str_var) for _ in range(16)])
random.choice(str_var)
This will provide you with a single random character from the string you have given.这将从您提供的字符串中为您提供一个随机字符。 The result maybe 'F'or 'g' or so on... So if you want the string of length 16 places you can iterate for 16 times.
结果可能是 'F' 或 'g' 等等......所以如果你想要长度为 16 位的字符串,你可以迭代 16 次。
result=""
for i in range(16):
result+=random.choice(str_var)
You can simply do.你可以简单地做。
import random
x = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
print(''.join(random.sample(x, k=16)))
Take an empty string variable.取一个空字符串变量。 Run a loop 16 times and using random module, select random character from
str_var
using random.choice()
and add it to empty string.运行循环 16 次并使用 random 模块,使用
random.choice()
从str_var
选择随机字符并将其添加到空字符串中。 Your code:您的代码:
import random
str_var = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
a=""
for i in range(16):
x=random.choice(str_var) #taking random character from str_var
a+=x
print(a)
import random
import string
rand_str = "".join(random.choice(string.ascii_letters) for _ in range(16))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.