简体   繁体   English

当我将列表转换为字符串时,为什么这会给我“TypeError:'int' 类型的参数不可迭代”

[英]Why is this giving me "TypeError: argument of type 'int' is not iterable" when I converted the list to strings

Why is this giving me:为什么这给了我:

"TypeError: argument of type 'int' is not iterable" “类型错误:‘int’类型的参数不可迭代”

when I converted the list to strings:当我将列表转换为字符串时:

def giveMeFive (start, end):
    numbers = list(range(start,end))
    for x in numbers:
        [str(x) for x in numbers]
        if str(5) in x:
            return x

u can use this code你可以使用这个代码

def giveMeFive (start, end):
    numbers = list(range(start,end))
    return numbers.index(5)

When you write [str(x) for x in numbers] all by itself, you are creating a new list of strings, and then immediately throwing it away.当您[str(x) for x in numbers]编写[str(x) for x in numbers] ,您正在创建一个新的字符串列表,然后立即将其丢弃。 You have to save it in a variable to keep it.您必须将其保存在一个变量中以保留它。

Try尝试

def giveMeFive (start, end):
    numbers = list(range(start,end))
    strings = [str(x) for x in numbers]
    for x in strings:
        if str(5) in x:
            return x

Now, there are more errors in your code, but hopefully this will get you unstuck.现在,您的代码中存在更多错误,但希望这能让您摆脱困境。

更清楚地说,此代码旨在返回包含字符串“5”的特定范围内的所有数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:类型为'int'的参数是否可迭代? - TypeError: argument of type 'int' is not iterable? 类型错误:“int”类型的参数不可迭代 - TypeError: argument of type 'int' is not iterable TypeError:“ int”类型的参数不可迭代 - TypeError: argument of type 'int' is not iterable TypeError:“ int”类型的参数不可迭代 - TypeError: argument of type 'int' is not iterable 我想知道为什么我会收到 TypeError: argument of type 'int' is not iterable - I'm wondering why I'm getting a TypeError: argument of type 'int' is not iterable TypeError:实现套接字程序时,'int'类型的参数不可迭代 - TypeError: argument of type 'int' is not iterable when implementing socket program 迭代 python 字典的键,当键是整数时,我收到此错误,“TypeError:'int' 类型的参数不可迭代” - Iterating over the keys of a python dictionary, when keys are integers, I get this error, "TypeError: argument of type 'int' is not iterable" Python TypeError:“int”类型的参数不是可迭代的问题 - Python TypeError: argument of type 'int' is not iterable problem TypeError:“int”类型的参数在 python 中不可迭代用于 dict - TypeError: argument of type 'int' is not iterable in python for a dict 获取 TypeError:'int' 类型的参数在 Python 中不可迭代 - Getting TypeError: argument of type 'int' is not iterable in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM