4 回答
TA贡献1797条经验 获得超4个赞
用这个改变你的 userGen() :
def userGen():
c = 0
upper = int(input("How many letters you want? "))
spec = int(input("How many symbols you want? "))
num = int(input("How many numbers you want? "))
c += upper + num +spec
if c < 7:
print('Your password is too short.')
userGen()
else:
return passGen(upper,spec,num)
编辑:
import random
import string
import secrets
length = 7
# This variable includes numbers
digits = "1234567890"
# This variable includes uppercase and lowercase letters
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
# This variable includes symbols
symbols = "!#$%&'()*+, -./:;<=>?@[\]^_`{|}~"
password = ''.join(secrets.choice(digits + letters + symbols) for t in range(20))
def message():
print("Hello, here is a password generator. ")
print("Choose the length of your characters:")
passGen()
def passGen():
c = 0
upper = int(input("How many letters you want? "))
spec = int(input("How many symbols you want? "))
num = int(input("How many numbers you want? "))
c += upper + num +spec
if c < length:
print('Your password is too short.')
passGen()
else:
new_password = ""
for i in range(upper):
new_password += random.choice(letters)
for x in range(spec):
new_password += random.choice(symbols)
for y in range(num):
new_password += random.choice(digits)
pass_word = list(new_password)
shuff = random.shuffle(pass_word)
new_pass = "".join(pass_word)
password = secrets.token_urlsafe(7)
print(new_pass)
message()
TA贡献1842条经验 获得超12个赞
我相信你想要完成的事情可以用一个简单的if
语句来完成。你想要:
检查用户是否输入了最多 7 个字符的组合
如果那是真的:
像您目前正在做的那样分配密码
如果不是这样,您至少有两个选择:
像@Ktoto 建议的那样打印一条错误消息
通过自己设置字母、数字和符号的数量来强制密码至少为 7 个字符。
TA贡献1851条经验 获得超5个赞
您可以像这样使用 if 语句:
def userGen():
upper = int(input("How many letters you want? "))
spec = int(input("How many symbols you want? "))
num = int(input("How many numbers you want? "))
if int(upper) + int(spec) + int(num) <= 7:
# your code
return passGen(upper, spec, num)
然后向您喜欢的特定类型的字符添加更多字符。
TA贡献1806条经验 获得超5个赞
如果用户的选择加起来不够,这个问题并不清楚应该添加什么样的字符,所以我建议你只是让用户再试一次,直到有足够的为止。
虽然严格来说不是问题的答案,但这里有一些关于您可以执行的代码一般整理的建议。它包括两个字典的使用:characters一个保存字符的类型,另一个choices保存用户对每种类型项目的数量的选择。如果您想更改内容,例如将“字母”拆分为大写和小写的单独计数,那么您只需要更改顶部的字典。
import random
characters = {'digits': '1234567890',
'letters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
'symbols': "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"}
def getChoices(min_total=7):
print('Hello, here is a password generator: ')
print('Choose the length of your characters?')
while True:
total = 0
choices = {}
for category in characters.keys():
number = int(input(f'How many {category} do you want? '))
choices[f'num_{category}'] = number
total += number
if total >= min_total:
return choices
else:
print(f'Need at least {min_total} characters - please try again')
def passGen(choices):
new_password = ''
for category, options in characters.items():
for i in range(choices[f'num_{category}']):
new_password += random.choice(options)
pass_word = list(new_password)
random.shuffle(pass_word)
new_pass = ''.join(pass_word)
return new_pass
def main():
choices = getChoices()
print(f'Your choices: {choices}')
password = passGen(choices)
print(f'Your password: {password}')
main()
添加回答
举报