[英]Why is this giving me "TypeError: argument of type 'int' is not iterable" when I converted the list to strings
為什么這給了我:
“類型錯誤:‘int’類型的參數不可迭代”
當我將列表轉換為字符串時:
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
你可以使用這個代碼
def giveMeFive (start, end):
numbers = list(range(start,end))
return numbers.index(5)
當您[str(x) for x in numbers]
編寫[str(x) for x in numbers]
,您正在創建一個新的字符串列表,然后立即將其丟棄。 您必須將其保存在一個變量中以保留它。
嘗試
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
現在,您的代碼中存在更多錯誤,但希望這能讓您擺脫困境。
更清楚地說,此代碼旨在返回包含字符串“5”的特定范圍內的所有數字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.