Python TypeError:只能将列表(而不是“int”)连接到列表。将两个变量指定给一个变量时出错

Python TypeError:只能将列表(而不是“int”)连接到列表。将两个变量指定给一个变量时出错,python,Python,我正在使用名为password的类和名为generate\u memberable(生成易于记忆的密码)和generate\u decomplex的两个函数制作一个密码生成器,这两个函数将生成一个复杂的密码 我的代码: import random import urllib.request import string class password: def __init__(self, length): self.length = length def gen

我正在使用名为
password
的类和名为
generate\u memberable
(生成易于记忆的密码)和
generate\u decomplex
的两个函数制作一个密码生成器,这两个函数将生成一个复杂的密码

我的代码:

import random
import urllib.request
import string

class password:
    def __init__(self, length):
        self.length = length

    def generate_intricate(self, iterations):
        characters = string.ascii_letters + string.digits + string.punctuation
        for p in range(iterations):
            output_password = ''
            for c in range(self.length):
                output_password += random.choice(characters)
            print(output_password)

    def generate_memorable(self, iterations):
        # get some random words
        word_url = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}
        req = urllib.request.Request(word_url, headers=headers)
        response = response = urllib.request.urlopen(req)
        long_txt = response.read().decode()
        words = long_txt.splitlines()

        output_password = ''

        # generate the number of password specified
        for i in range(iterations):
            while len(output_password) != self.length:

                # generate a random number with a length of 3 to 5
                for i in range(random.randint(3, 5)):
                    numbers = random.randint(1, 9)

                # the ouput password is equal to the words from the world_url and numbers put together
                output_password = words + numbers # THIS IS WHERE THE PROBLEM IS OCCURING

                # im trying to make sure that the length of the generated password is equal to the length specified by the user
                if len(output_password) > self.length: # if length of password is larger than length specified
                    difference = len(output_password) - self.length # difference between output password and length specified
                    print(output_password[: -difference]) # print the output password minus the difference.  

                elif len(output_password) < self.length: # if length of output_password is smaller than length specified
                    difference = self.length - len(output_password) # difference = length specified - length of output_password
                    print(output_password,output_password[: difference]) # print output_password + difference characters from output_password. 

                elif len(output_password) == self.length: # if length of output_password = length specified
                    print(output_password)

# Test
password1 = password(20) # password length = 20
password1.generate_memorable(3) # generate a memeorable password (containing words and numbers) 3 times
随机导入
导入urllib.request
导入字符串
类别密码:
定义初始值(自身,长度):
self.length=长度
def生成复杂(自身、迭代):
字符=string.ascii_字母+string.digits+string.标点符号
对于范围内的p(迭代):
输出密码=“”
对于范围内的c(自身长度):
输出\密码+=随机选择(字符)
打印(输出密码)
def生成(自我、迭代):
#得到一些随机的单词
word_url=”http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-类型=文本/普通“
headers={'User-Agent':'Mozilla/5.0(WindowsNT6.1;Win64;x64)}
req=urllib.request.request(word\u url,headers=headers)
response=response=urllib.request.urlopen(req)
long_txt=response.read().decode()
words=long_txt.splitlines()
输出密码=“”
#生成指定的密码数目
对于范围内的i(迭代):
而len(输出_密码)!=自我长度:
#生成一个长度为3到5的随机数
对于范围内的i(random.randint(3,5)):
数字=random.randint(1,9)
#输出密码等于world_url中的单词和数字的总和
输出密码=单词+数字#这就是问题所在
#我正在努力确保生成的密码的长度等于用户指定的长度
if len(output_password)>self.length:#如果密码长度大于指定的长度
差异=len(输出密码)-self.length#输出密码和指定长度之间的差异
打印(输出密码[:-差异])#打印输出密码减去差异。
elif len(output_password)
我的问题

当我运行该程序时,出现以下错误:

回溯(最近一次呼叫最后一次):
文件“C:\Users\sbenf\OneDrive\Python Projects\Large Projects\Adventure\u Colussus\u Game\passwordtest.py”,第55行,在
密码1.生成可记忆(3)#生成可记忆密码(包含单词和数字)3次
文件“C:\Users\sbenf\OneDrive\Python Projects\Large Projects\Adventure\u Colussus\u Game\passwordtest.py”,第37行,在generate\u中
输出密码=单词+数字#这就是问题所在
TypeError:只能将列表(而不是“int”)连接到列表

我不知道该怎么办,所以我想知道是否有人能给我指出正确的方向。

您正在尝试向
int
添加
列表(
单词
),即使这两种类型都不是要添加的。要解决此问题,必须将
int
转换为
列表
,如下所示

output_password = words + [numbers]
另一点是:您的代码没有生成长度为3到5的数字。由于您只是在循环
中对范围内的i(random.randint(3,5)):numbers=random.randint(1,9)
迭代3次,而不保存随机生成器的结果,因此它将始终覆盖
numbers
变量。您必须首先定义
numbers
为空列表,然后将生成的随机数附加到其中,如下所示:

numbers = []
for i in range(random.randint(3,5)):
    numbers.append(random.randint(1,9))

既然
numbers
是一个列表,它可以添加到
words
中,而不会出现进一步的问题

如果
output\u password
的最终目标是一个字符串,那么您需要加入单词列表并在单词后面附加int(转换为字符串)

mylist=[“你好”,“世界”]
数字=123
输出_password=“.join(单词)+str(数字)
产出:

helloworld123

在抛出错误的行之前执行的循环是无用的,因为您只存储上一次迭代的结果。请注意,
numbers
将是一个位数。您想写入
numbers=random。randint(10099999)
words
是一个列表,
numbers
是一个整数。你不能用加号把这两件事加起来。请阅读。您还可以使用它来帮助逐步可视化代码的执行。我要说的是,关于这个问题,错误是非常清楚的:
words
是一个列表(从
long\u txt.splitlines()获取)(
),而
numbers
是一个int(从
random.randint(1,9)
),或者执行
words.append(numbers)
或者根据its的名称,可能
数字实际上应该是
数字=[random.randint(1,9)表示范围内的i(random.randint(3,5))
在这种情况下,添加将起作用(添加两个列表),它只需向
long_txt
的整个内容添加1 int,这在技术上避免了错误,但不会修复逻辑