簡體   English   中英

有沒有辦法減少ifs的數量?

[英]Is there a way to reduce the number of ifs?

我正在學習 Python 的基礎知識並正在編寫密碼生成器。 它應該詢問用戶他們想要什么類型的密碼以及所需的長度。 有沒有辦法縮短下面的代碼? 我想我使用了很多ifs。 非常感謝任何反饋。

`import random
import string

ans_n = input('Do you want numbers in your password?(y/n) ')
ans_l = input('Do you want letters in your password?(y/n) ')
ans_s = input('Do you want special characters in your password?(y/n) ')


def random_numbers(user_numbers_length):
    digits = []
    for i in range(user_numbers_length):
        digits.append(str(random.randint(1, 10)))

    return digits


def random_letters(user_letter_length):
    return random.sample(string.ascii_letters, user_letter_length)


def random_characters(user_special_length):
    stringSpecial = []
    for i in range(user_special_length):
        stringSpecial.append(random.choice('!$%&()*+,-.:;<=>?@[]^_`{|}~'))

    return stringSpecial

if ans_l == 'y' and ans_n == 'n' and ans_s == 'n':
    print("how many letters do you want in your password?")
    user_letter_length = input()
    password = random_letters(int(user_letter_length))

if ans_l == 'y' and ans_n == 'y' and ans_s == 'n':
    print("how many letters do you want in your password?")
    user_letter_length = input()
    print("how many numbers do you want in your password?")
    user_numbers_length = input()
    password = random_letters(int(user_letter_length)) + random_numbers(int(user_numbers_length))

if ans_l == 'y' and ans_n == 'y' and ans_s == 'y':
    print("how many letters do you want in your password?")
    user_letter_length = input()
    print("how many numbers do you want in your password?")
    user_numbers_length = input()
    print("how many special characters do you want in your password?")
    user_special_length = input()
    password = random_letters(int(user_letter_length)) + random_numbers(int(user_numbers_length)) + random_characters(
        int(user_special_length))

if ans_l == 'n' and ans_n == 'y' and ans_s == 'n':
    print("how many numbers do you want in your password?")
    user_numbers_length = input()
    password = random_numbers(int(user_numbers_length))

if ans_l == 'n' and ans_n == 'y' and ans_s == 'y':
    print("how many numbers do you want in your password?")
    user_numbers_length = input()
    print("how many special characters do you want in your password?")
    user_special_length = input()
    password = random_numbers(int(user_numbers_length)) +random_characters(int(user_special_length))

if ans_l == 'n' and ans_n == 'n' and ans_s == 'y':
    print("how many special characters do you want in your password?")
    user_special_length = input()
    password = random_characters(int(user_special_length))

if ans_l == 'y' and ans_n == 'n' and ans_s == 'y':
    print("how many letters do you want in your password?")
    user_letter_length = input()
    print("how many special characters do you want in your password?")
    user_special_length = input()
    password = random_letters(int(user_letter_length)) +random_characters(int(user_special_length))

random.shuffle(password)

print(f"Your generated password is: ")
print(''.join(password))
`

與其問很多問題,如果你知道你將不得不詢問每個角色類別,你可以提前問數字。 如果他們不想要,您可以建議他們輸入0 然后是使用k=#指定的數字調用random.choices()的問題。 最后,您在該過程結束時改組密碼。

這種方法還最大限度地減少了用戶必須回答的問題數量。 如果用戶想要字母,他們可以告訴你他們想要多少,而不必告訴你是,然后告訴你有多少。

import random
import string

ans_l = int(input("How many letters do you want (0 for none)? "))
ans_n = int(input("How many numbers do you want (0 for none)? "))
ans_s = int(input("How many specials do you want (0 for none)? "))

password = []
password += random.choices(string.ascii_letters, k=ans_l)
password += random.choices(string.digits, k=ans_n)
password += random.choices(string.punctuation, k=ans_s)

random.shuffle(password)
print(''.join(password))

示例交互看起來像......

~$ python3 generate_password.py 
How many letters do you want (0 for none)? 4
How many numbers do you want (0 for none)? 4
How many specials do you want (0 for none)? 4
5N+46d9I_\"Z

或者沒有特殊字符

~$ python3 so.py 
How many letters do you want (0 for none)? 6
How many numbers do you want (0 for none)? 6
How many specials do you want (0 for none)? 0
9N0Y5X0p29yG

由於您的random_numbersrandom_lettersrandom_characters函數僅在它們使用的字符集上有所不同,因此您可以使用 dict 將顯示名稱映射到字符集,然后遍歷 dict 項目以請求輸入:

import random
import string

character_sets = {
    'numbers': string.digits,
    'letters': string.ascii_letters,
    'special characters': string.punctuation
}

characters = []
for name, value in character_sets.items():
    if input(f'Do you want {name} in your password?(y/n) ') == 'y':
        characters.extend(random.choices(value, k=int(input(f'How many {name} do you want in your password? '))))
random.shuffle(characters)
print(f'Your generated password is : {"".join(characters)}')

只是為了類似:

user_letter_length = None
user_numbers_length = None
user_special_length = None

if ans_l == 'y':
    print("how many letters do you want in your password?")
    user_letter_length = input()

if ans_n == 'y':
    print("how many numbers do you want in your password?")
    user_numbers_length = input()

if ans_s == 'y':
    print("how many special characters do you want in your password?")
    user_special_length = input()

然后當您創建密碼時,只需檢查user_letter_lengthuser_numbers_lengthuser_special_length任何一個user_letter_lengthNone 如果任何變量為None ,那是因為用戶沒有設置它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM