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條經驗 獲得超13個贊
我相信你想要完成的事情可以用一個簡單的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()
添加回答
舉報