迭代一个范围的次数超过生成所需输出所需的次数是否有任何缺点?

Is there any downside to iterating over a range for more times than is required to generate desired output?

我正在 Python 学习 100 天代码,我正在尝试通过接收用户输入的字母、数字和符号数量来创建 Python 密码生成器他们希望输入密码。

下面的程序运行并生成所需的输出,但我知道肯定有比在范围内迭代任意次数以生成固定长度密码更好的方法。

import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input('How many letters would you like in your password?: ')) 
nr_symbols = int(input('How many symbols would you like?: '))
nr_numbers = int(input('How many numbers would you like?: '))

password = ""

# Generate unshuffled password

# for i in range(1, (nr_letters + 1)):
#   password += random.choice(letters)
# for i in range(1, (nr_symbols + 1)):
#   password += random.choice(numbers)
# for i in range(1, (nr_numbers +1)):
#   password += random.choice(symbols)
# print(password)

letter_counter = 0
symbol_counter = 0
number_counter = 0

# NOTE: This seems dumb but it works so...

for i in range(0, 100):
  random_int = random.randint(0, 2)
  if random_int == 0 and letter_counter < nr_letters:
    password += random.choice(letters)
    letter_counter += 1
  elif random_int == 1 and symbol_counter < nr_symbols:
    password += random.choice(symbols)
    symbol_counter += 1
  elif random_int == 2 and number_counter < nr_numbers:
    password += random.choice(numbers)
    number_counter += 1

print(password)

有没有一种更简洁的方法可以通过 Python for 循环创建打乱的固定长度字符串?

对于未来,循环迭代次数超过生成所需输出的次数是否有重大缺点?

我想这就是您要找的东西?:

import random
letters = ['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', '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']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input('How many letters would you like in your password?: ')) 
nr_symbols = int(input('How many symbols would you like?: '))
nr_numbers = int(input('How many numbers would you like?: '))

password = ""

for i in range(1, sum([nr_letters,nr_numbers,nr_symbols])+1):
  if i<= nr_letters:
    password += random.choice(letters)
  elif i > nr_letters and i<=nr_symbols+nr_letters:
    password += random.choice(symbols)
  elif i > nr_symbols+nr_letters:
    password += random.choice(numbers)

password=[x for x in password]
random.shuffle(password)
password="".join(password)
print(password)

基本上,它不是遍历任意数字,而是将所需的字符数相加,然后遍历范围,并在满足要求时添加 numbers/symbols/letters。

基本要求。当低于或等于所需的字母数时,添加一个字母。然后,你去把字母和符号的个数加在一起,说要满足字母个数以上,字母+符号以下。然后,如果i上面是字母+符号,就加上数字。大致就是这样。

缺点是你必须像我上面那样洗牌。 password=[x for x in password] 基本上把 password 变成了一个列表。然后你用 random.shuffle(password) 洗牌,然后加入 password="".join(password).

这基本上是您在 1 中捆绑的第一个注释掉的迭代。