这是一个生成3个随机字母、数字和符号的代码。我试着用"if“公式来避免三个字母后面的点,它对我起了作用:
if password[length-1] == dot:
dot_password = password.replace(password[length-1],"" .join(random.sample(alll,new_length)))
file.write(dot_password+"\n")但是,当我试图避免只生成3个数字时,我没有这样做:
elif password == NUMBERS:
number_password = password.replace(password,"" .join(random.sample(allll,length)))
file.write(number_password+"\n")所以我才来找你的解决方案。有关信息,这是Python语言
这是完整的代码:
import random
file = open(r"C:\******\******\******\******\******\******\******\Users.txt", "r+")
for i in range(10000): # I set it to 10000 to test only.
lower = "abcdefghijklmnopqrstuvwxyz_"
NUMBERS = "0123456789"
dot = "."
all = lower + NUMBERS + dot
alll = lower + NUMBERS
allll = lower + dot
length = 3
new_length = 1
password = "".join(random.sample(all,length))
if password[length-1] == dot:
dot_password = password.replace(password[length-1],"" .join(random.sample(alll,new_length)))
file.write(dot_password+"\n")
elif password == NUMBERS:
number_password = password.replace(password,"" .join(random.sample(allll,length)))
file.write(number_password+"\n")
else:
file.write(password+"\n")发布于 2021-12-18 20:42:55
这里的NUMBERS = '0123456789'数字是一个数字字符串。
elif password == NUMBERS:这里的密码必须为“0123456789”才能使此条件变为真
这就是为什么上述代码不能工作的原因。
试试看
elif password.is_numeric():测试整个密码是否只包含数字
发布于 2021-12-18 20:37:43
我假设您想知道生成的密码是否只包含数字:
def is_all_number(password):
numbers = '1234567890'
for i in password:
if not i in numbers:
return False
return True然后你可以在你的条件下使用它:
if is_all_number(password):
...或者您可以使用password.isdecimal()作为条件。
https://stackoverflow.com/questions/70406835
复制相似问题