簡體   English   中英

隨機密碼生成器只生成有限數量的字符

[英]Random password generator only generating limited amount of characters

我正在學習 Python,今天我決定嘗試使用 go 創建隨機密碼生成器。 該代碼生成隨機數量的字母,然后是數字,然后是符號(基於用戶指定的數量)。 這些值都存儲在一個列表中,其中每個值都相互連接。 從那里所有的值都被重新隨機化以產生最終密碼。

如果我嘗試使用大數字作為輸入(大約 40 以上),則會出錯。 它也不總是產生指定的長度。 比如我輸入30個字母,30個數字,30個符號,如果我沒記錯的話,最后的密碼就是少了90乘以10左右。 任何幫助將不勝感激——我認為修復很簡單,只是找不到它。

Here is my code: 


import random
import itertools

# lists
chars = ['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']
nums = ['0','1','2','3','4','5','6','7','8','9']
syms = ['!','@','#','$','%','^','&','*','+','_']

# inputs
print("Python Password Generator : \n")
charAmount = int(input("How many letters would you like to have in your password? : "))
numAmount = int(input("How many numbers? : "))
symAmount = int(input("How many symbols? : "))

# random num/sym/char

randomNum = ""
for num in nums :
  if int(num) < numAmount :
    randomNum += nums[random.randint(0,9)]


randomSym = ""
for sym in range(0, symAmount):
  if sym < symAmount :
    randomSym += syms[random.randint(0,9)]


randomChar = ""
for char in range(0, charAmount) :
  if sym < charAmount :
    randomChar += chars[random.randint(0, 25)]


# Final Password generator
generatorList = list(itertools.chain(randomNum,randomSym,randomChar))

Password = ""
tot = len(generatorList)
for word in generatorList:
      Password += generatorList[random.randint(0,tot)]

print(f"Your new password is : {Password}")

您的代碼有兩個問題:

  1. if語句不是必需的,因為迭代次數已經受到for循環的限制。
  2. random.randint()兩邊都包含 arguments。 所以,在這個代碼片段中:
Password = ""
tot = len(generatorList)
for word in generatorList:
      Password += generatorList[random.randint(0,tot)]

.randint()可以返回tot ,這超出了generatorList的范圍。

這是解決這兩個問題的代碼片段:

import random
import itertools
# lists
chars = ['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']
nums = ['0','1','2','3','4','5','6','7','8','9']
syms = ['!','@','#','$','%','^','&','*','+','_']

# inputs
print("Python Password Generator : \n")
charAmount = int(input("How many letters would you like to have in your password? : "))
numAmount = int(input("How many numbers? : "))
symAmount = int(input("How many symbols? : "))

# random num/sym/char

randomNum = ""
for num in range(numAmount) :
    randomNum += nums[random.randint(0,9)]

randomSym = ""
for sym in range(0, symAmount):
    randomSym += syms[random.randint(0,9)]

randomChar = ""
for char in range(0, charAmount) :
    randomChar += chars[random.randint(0, 25)]

# Final Password generator
generatorList = list(itertools.chain(randomNum,randomSym,randomChar))

Password = ""
tot = len(generatorList)
for word in generatorList:
     Password += generatorList[random.randint(0,tot - 1)]

print(f"Your new password is : {Password}")

這些可以解決您眼前的問題,但我們還可以進行一些額外的改進:

  1. 我們可以使用string模塊中的常量,而不是列出小寫字符和數字。
  2. 重復的字符串連接效率低下 請改用.join()
  3. 使用random.choices()來選擇要添加到我們密碼中的字符,然后在最后將它們全部洗牌。

考慮到所有這些,我們得到以下信息:

import random
import string

chars = string.ascii_lowercase
nums = string.digits
syms = ['!','@','#','$','%','^','&','*','+','_']

print("Python Password Generator : \n")
char_amount = int(input("How many letters would you like to have in your password? : "))
num_amount = int(input("How many numbers? : "))
sym_amount = int(input("How many symbols? : "))

password_characters = random.choices(chars, k=char_amount) + random.choices(nums, k=num_amount) + random.choices(syms, k=sym_amount)
random.shuffle(password_characters)

password = ''.join(password_characters)
print(f"Your new password is : {password}")

暫無
暫無

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

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