简体   繁体   中英

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"

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. 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”的特定范围内的所有数字。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM