本文介绍了类型错误:“'types.GenericAlias' 对象不可迭代";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始了一个新项目(仍然是编码新手).该项目的当前目标是将文本转换为数字.(例如,hello world"将是8 5 12 12 15 27 23 15 18 12 4").在第 10 行,for 循环导致错误消息:TypeError: 'types.GenericAlias' object is not iterable".我在网上找不到任何提及此错误的信息.

I just got started on a new project (still new to coding). The current aim of this project is to convert text into numbers. (For example, "hello world" would be "8 5 12 12 15 27 23 15 18 12 4"). In line 10, the for loop causes an error message: "TypeError: 'types.GenericAlias' object is not iterable". I could not find any mention of this error online.

alphabet = ("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')


def getplaintext():
    global plaintext
    plaintext = list[input("Enter plaintext:.....")]
    print(plaintext)

def converter(plaintext):
    for n in plaintext:
        print(n)


getplaintext()
converter(plaintext)

这会导致以下错误:

Traceback (most recent call last):
  File "C:\Users\gabri\PycharmProjects\padding\main.py", line 15, in <module>
    converter(plaintext)
  File "C:\Users\gabri\PycharmProjects\padding\main.py", line 10, in converter
    for n in plaintext:
TypeError: 'types.GenericAlias' object is not iterable

Process finished with exit code 1

有人知道这是什么原因吗?

Does anybody know what is causing this?

推荐答案

您想要:

plaintext = list(input("Enter plaintext:....."))  # With ()

使用需要使用(),而不是[].在较新版本的 Python 中,可以直接提示容器类型以指定元素类型.通过在那里使用方括号,它认为您正在尝试创建列表的泛型类型提示,从而导致错误.

Use need to use (), not []. In newer versions of Python, container types can be hinted directly to specify the element type. By using square braces there, it thinks that you're attempting to create a generic type hint of a list, thus the error.

这篇关于类型错误:“'types.GenericAlias' 对象不可迭代";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:36