【发布时间】:2014-05-10 15:19:59
【问题描述】:
我使用的是 python 101 4.1 版
输入示例为 (1,4,6,2,53,7)
需要的输出是偶数(2,4,6)奇数(1,7,53)
我需要在这个问题中使用一个只有 1 个输入的函数,这将是列表,我认为循环会有所帮助,但我仍然无法得到它。我尝试使用以下代码:
from math import *
from string import *
def odd_even(L):
list1=raw_input()
list1=list1.split(" ")
even=[]
odd=[]
for x in list1:
if x%2==0:
even.append(L)
else:
odd.append(L)
return even,odd
L=input()
print odd_even(L)
【问题讨论】:
-
你为什么不编辑你之前的问题?
-
@EduardGamonal:不一定,这个有基于答案之一的代码.. 边缘情况,我会说。
-
为什么在函数中使用更多的输入调用?你在这里也对
L和list1感到困惑。 -
另外,不需要导入
math和string模块。
标签: python python-2.7 for-loop infinite-loop