当前位置: 首页 > 知识库问答 >
问题:

TypeError:只能将列表(而不是“int”)连接到列表。将2个变量分配给一个变量时出错

许明朗
2023-03-14

我要做的事

我正在使用一个名为password的类和一个名为generate_memorable(生成一个容易记住的密码)和generate_intractice的函数来生成一个密码生成器,这两个函数将生成一个复杂的密码。

我的代码:

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

我的问题

当我运行这个程序时,我得到一个错误:typeerror:只能将列表(不是“int”)连接到列表

完全错误:

回溯(最近一次调用):文件“C:\users\sbenf\oneDrive\python projects\large projects\adventure_colussus_game\passwordtest.py”,第55行,在password1.generate_memorable(3)#生成一个可记忆密码(包含单词和数字)3次文件“C:\users\sbenf\oneDrive\python projects\large projects\adventure_colussus_game\passwordtest.py”,第37行,在generate_memorable output_password

将错误分解为多个位,以便于读取:

回溯(最近一次调用):文件“C:\users\sbenf\OneDrive\python projects\large projects\adventure_colussus_game\passwordtest.py”,

password1.generate_memorable(3)#生成可记忆密码(包含单词和数字)3次

文件“C:\users\sbenf\onedrive\python projects\large projects\adventure_colussus_game\passwordtest.py”,第37行,在generate_memorable中

output_password=words+numbers#这是发生问题的地方

TypeError:只能将列表(而不是“int”)连接到列表

我不知道该怎么做,所以我想知道是否有人能给我指明正确的方向。非常感谢:D

共有2个答案

许兴文
2023-03-14

您试图将列表(单词)添加到int中,尽管这两种类型都不是要添加的。要解决此问题,您必须将int转换为list,如下所示

output_password = words + [numbers]
孙佑运
2023-03-14

如果output_password的最终目标是字符串,那么您需要加入单词列表并在后面添加int(转换为字符串)。

mylist = ["hello", "world"]
numbers = 123
output_password = "".join(words) + str(numbers)

产出:

helloworld123
 类似资料:
  • 以下代码编译确定: 但是嵌套列表不会: 这是为什么?如果我需要这样做,“正确”的方法是什么? 用例是我有一个方法,它返回许多不同集合中的一个,其中元素类型本身是泛型的,每个可能的返回值都有不同的定义类型,其中返回值取决于传入的参数。目前,作为一种解决方法,我将结果分配给一个变量,其中类型是带有通配符的集合类型(即<code>列表

  • 问题内容: 我想了解原始和对象引用变量的行为方式不同。我以Kathy Sierra的 OCA / OCP Java SE7中 的以下代码为例: 在上面的代码中,我获得了更改之前和之后的值。 基本变量case的输出为: 但是,在对象引用变量中,一旦更改了的值,我将获得不同的值 参考变量大小写的输出为: 书中提到在两种情况下都复制位模式并放置新副本。如果这是真的,那为什么我们会得到不同的行为呢? 问题

  • 问题内容: 在Python中,我们可以将函数分配给变量。例如,math.sine函数: 有没有简单的方法可以将多个函数(即一个函数的一个函数)分配给一个变量?例如: 问题答案: 我认为作者想要的是某种形式的功能链。通常,这很困难,但是对于 接受一个论点 返回一个值, 列表中上一个函数的返回值与列表中下一个函数的输入类型具有相同的返回值 我们说有一个需要链接的函数列表,其中有一个参数,然后返回一个参

  • 所以我有两个函数都有相似的参数 现在,调用,但我如何在不修改的情况下传递变量参数列表中的变量(因为这在其他地方也已经使用了)。

  • 我有这样一份清单: 如何将此列表拆分为三个变量,每个变量分别保持不变

  • 但是如果我有超过20个变量和非常大的行数呢。在data.table中有没有类似于。sd的东西可以让我在分组数据框架中获取所有变量的平均值?或者,是否可以在分组的数据帧上使用lapply? 谢谢你的帮助