简体   繁体   English

有人可以帮助我使用我的随机代码生成器吗?

[英]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.

相关问题 有人可以帮我理解这段代码吗? [关闭] - Can someone help me understanding this code please? [closed] 有人可以帮我处理我的 Python 代码吗? - Could someone please help me with my Python code? 请问有人可以帮我整理我的代码吗? - Please could someone help me to tidy up my code? 有人可以帮我写代码吗? (Python) - Can someone help me with my code? (python) 有人可以帮我找出缺少的部分来执行此代码吗? - Can someone please help me identify the missing piece to execute this code? 有人可以帮我制作重叠的直方图吗? - Can someone please help me make my overlapping histograms? 在我的代码中,为什么我的列表没有从最早到最晚的日期排序? 有人可以帮我解决这个问题吗? - In my code, why doesn't my list sort from the earliest to the latest in dates? Can someone please help me solve this problem? 有没有办法检测消息中的链接? (或者有人可以帮我写代码) - Is there a way to detect a link in a message? (or can someone help me with my code) 有人可以帮我找到代码中错误源的解决方案吗? - Can someone help me find a solution to error source in my code? 有人可以向我解释这些代码行吗? - Can someone please explain me these lines of code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM