“计划”古芬.py“要求用户提供一个列表,并从列表中删除奇数并打印出新列表。这是我的代码:
def remodds(lst):
result = []
for elem in lst:
if elem % 2 == 0: # if list element is even
result.append(elem) # add even list elements to the result
return result
justaskin = input("Give me a list and I'll tak out the odds: ") #this is
#generates
#an EOF
#error
print(remodds(justaskin)) # supposed to print a list with only even-
# numbered elements
#I'm using Windows Powershell and Python 3.6 to run the code. Please help!
#error message:
#Traceback (most recent call last):
# File "goofin.py", line 13, in <module>
# print(remodds(justaskin))
# File "goofin.py", line 4, in remodds
# if elem % 2 == 0:
#TypeError: not all arguments converted during string formatting
这对我来说很好:
我的意见:
^{pr2}$我的输出:
说明:
希望这有帮助!在
您的输入}之类的列表。当你把它和你的
lst
不是一个列表,即使您键入2, 13, 14, 7
或{elem
循环分开时,它仍然是一个字符串,这意味着每个单独的字符都是一个循环。您必须首先拆分lst
,然后将它们转换成数字。在split方法目前使用的是数字之间的空格,但您也可以定义元素之间是用逗号分隔的。在
^{pr2}$相关问题 更多 >
编程相关推荐