【发布时间】:2017-11-29 14:59:40
【问题描述】:
程序“goofin.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
【问题讨论】:
-
您是否在有机会输入任何内容之前、在您按 Enter 之后或在其他时间收到错误?
-
在 Windows Powershell 中运行程序时出现错误。 IE。在我点击进入后
-
发布您的错误。在您的问题中。
-
在
for elem in lst: if elem % 2 == 0:之间放置一个print(elem, type(elem)),您将立即看到您的问题所在。取决于您输入的内容 a) 一个问题是您的elem是一个字符串 b) 您还有其他列表元素,例如space、,或[]。