input_list = ['tacocat', 'bob', 'davey']
def palindromes(input_list):
for word in input_list:
if input_list[word]==reversed(input_list[word]):
print("True")
else:
print("False")
output=palindromes(input_list)
print(output)
输出应该是[True, True, False]
但这给了我错误
Tags:
这是您的代码修复程序,存在许多问题:
return
语句)input_list
(只需使用word
)reversed
确实返回迭代器,因此匹配项始终为False(改用[::-1]
)也就是说,这里有一个简短的版本:
输出:
如果您编写返回语句而不是打印,那么它就可以工作了。还可以尝试只调用函数而不指定任何变量
反转字符串并检查
输出
相关问题 更多 >
编程相关推荐