【发布时间】:2017-02-05 17:26:37
【问题描述】:
我的代码返回“无”。 如果我的问题不清楚,如果我选择列表 [1 , 3 , 4 , 5 ,5 , 7],我希望返回列表 [1 , 3 , 4 , 7] 。我的代码如下:
print("This program takes a list of 5 items and removes all elements of 5: ")
list4 = []
list4.append(input("Please enter item 1:"))
list4.append(input('Please enter item 2:'))
list4.append(input('Please enter item 3:'))
list4.append(input('Please enter item 4:'))
list4.append(input('Please enter item 5:'))
def remove_five():
while 5 in list4:
list4.remove(5)
print(remove_five())
【问题讨论】:
-
你读的是字符串。您尝试删除的是
int。此外,您的remove_five()函数不会返回任何内容。 -
你打印了一个什么都不返回的方法(所以 None)
-
remove_five不返回任何内容 -
我想还没有人提到这个,但是
remove_five没有返回任何东西:-P -
您正在检查一个 int。您需要检查字符串 '5'
标签: python