尝试将用户定义的函数结果转换为 python 中的列表

Trying to convert user define function result into list in python

#Password Generator Project
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?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

#Eazy Level - Order not randomised:

#e.g. 4 letter, 2 symbol, 2 number = JduE&!91

def my_function():
  for i in range(1,nr_letters+1):
    variable=random.choice(letters)
    print(variable, end='')

  for j in range(1,nr_symbols+1):
    variable1=random.choice(symbols)
    print(variable1, end='')

  for k in range(1,nr_numbers+1):
    variable2=random.choice(numbers)
    print(variable2, end='')

#Hard Level - Order of characters randomised:

#e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P

#my_function()

function_to_list=my_function()
print[(function_to_list)]
shuffle_my_function=random.shuffle(function_to_list)
print(shuffle_my_function)

这是个人项目,我的任务是生成密码。在简单级别上,只需明智地打印密码序列,而在困难级别上,我希望我的密码被洗牌。 我的简单级别代码运行良好,但在困难级别上,我想打乱简单级别的结果,因为我认为如果我定义简单部分的函数,然后以某种方式将该函数转换为我可以轻松使用的列表随机播放功能。所以请帮助我。请尝试以我的思维方式给出解决方案,然后请建议您的做法

这是初学者常见的问题。当您打印某些内容时,它会出现在屏幕上,但只是文本形式。并且该程序看不到该文本。您必须对变量做一些事情才能随时跟踪它们。在这种情况下,您需要将它们附加到列表中。不仅如此,您还需要 return 列表给调用者,以便 function_to_list = my_function()None 以外的内容分配给 function_to_list:

def my_function():
    list_of_characters = []

    for i in range(nr_letters):
        list_of_characters.append(random.choice(letters))

    for j in range(nr_symbols):
        list_of_characters.append(random.choice(symbols))

    for k in range(nr_numbers):
        list_of_characters.append(random.choice(numbers))

    return list_of_characters 

注意我取出了打印语句。那是因为一个函数应该只做一件事并且把它做好。您可以在取回列表和密码后立即打印它们:

list_from_function = my_function()
print(list_from_function)

要将列表打印为单个字符串,请将其包含的字母与空字符串连接起来:

print(''.join(list_from_function))

你可以随机播放结果,或者做任何你想做的事:

random.shuffle(list_from_function)
print(list_from_function)

请记住 shuffle 在 return 和 None 中运行。这意味着如果您尝试打印其 return 值,您将一无所获。

您不需要使用 for loop。您可以将参数传递给 random.choices() 以指示您想要多少项。

import random

# For demo, I hardcoded the numbers
nr_letters = 4
nr_symbols = 5
nr_numbers = 3

# create a list from randomly choosen characters
password_characters = random.choices(letters, k = nr_letters) \
                        + random.choices(symbols, k = nr_symbols) \
                        + random.choices(numbers, k = nr_numbers)

# shuffle the list:
random.shuffle(password_characters)

# convert the list to string
password = ''.join(password_characters)

输出:

>>> print(password)
>>> &J0*4oR!I3$!