如何 select 随机字符串的值和长度?
How to select the values and the length of a random string?
我在字符串中列出了我想要的字符,也知道长度。就是不知道在哪里公布角色
我想使用。`
import random
characters = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z", ":", ";", "<", "=", ">", "?", "@", "[",
"]", "/", "^", "_", "'")
class Password:
def creat_password(self):
length = float(input("Enter a float between 6 & 12"))
您可以只创建一个空列表并遍历任意数量的随机字符并将它们附加到列表中,然后将它们连接成一个字符串并 return.
此外,您真的想使用 int(input("Enter a int between 6 & 12 "))
而不是 float(input("Enter a float between 6 & 12"))
,因为范围只接受整数而不接受浮点数。
def creat_password(self):
length = int(input("Enter a int between 6 & 12 "))
pw_chars = []
for i in range(length):
pw_chars.append(secrets.choice(characters))
password = ''.join(pw_chars)
return password
这个函数也可以使用理解表达式简化为:
def creat_password(self):
length = int(input("Enter a int between 6 & 12 "))
return ''.join(secrets.choice(characters) for i in range(length))
我在字符串中列出了我想要的字符,也知道长度。就是不知道在哪里公布角色 我想使用。`
import random
characters = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z", "A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z", ":", ";", "<", "=", ">", "?", "@", "[",
"]", "/", "^", "_", "'")
class Password:
def creat_password(self):
length = float(input("Enter a float between 6 & 12"))
您可以只创建一个空列表并遍历任意数量的随机字符并将它们附加到列表中,然后将它们连接成一个字符串并 return.
此外,您真的想使用 int(input("Enter a int between 6 & 12 "))
而不是 float(input("Enter a float between 6 & 12"))
,因为范围只接受整数而不接受浮点数。
def creat_password(self):
length = int(input("Enter a int between 6 & 12 "))
pw_chars = []
for i in range(length):
pw_chars.append(secrets.choice(characters))
password = ''.join(pw_chars)
return password
这个函数也可以使用理解表达式简化为:
def creat_password(self):
length = int(input("Enter a int between 6 & 12 "))
return ''.join(secrets.choice(characters) for i in range(length))