[英]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.