1

i'm using python 101 version 4.1

Example of input is (1,4,6,2,53,7)

Needed output is even(2,4,6) odd(1,7,53)

I need to use a function in this question with 1 input only which will be the list, i think a loop will help but i can't still get it. i tried using the following code:

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)
Brandon
  • 16,382
  • 12
  • 55
  • 88

5 Answers5

1

Input numbers and sort them:

def odd_even(numbers):
    result = [], []
    for number in numbers:
        result[number%2].append(number)
    return result

def main():
    numbers = raw_input('Enter numbers:')
    numbers = map(int, numbers.split())
    even, odd = odd_even(numbers)
    print "even", even
    print "odd", odd

if __name__ == '__main__':
    main()
Daniel
  • 42,087
  • 4
  • 55
  • 81
1

Here is solution that returns dictionary with keys "even" and "odd":

def odd_even(L):
   even=[num for num in L if num % 2 == 0]
   odd=[num for num in L if num % 2 != 0]
   return {"even": even, "odd": odd}

dict = odd_even([1,2,3,4,5])
print dict["even"]
print dict["odd"]
Josip Grggurica
  • 421
  • 4
  • 12
0

It is good you posted some code now; it would have been better to edit the first question.

You are nearly there:

# Module import is not necessary.

def odd_even(L):
    #    list1=raw_input()       # <--- no need for raw input here
    #    list1=list1.split(" ")  # <--- nor here
    even=[]
    odd=[]
    for x in L:
        if x%2==0:
            even.append(x)
        else:
            odd.append(x)
    return even,odd

L= range(20)                      # <--- can replace with raw_input if you need to
print odd_even(L)
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • I think Martijn Pieters edited my post at the same time I was editing it, then I messed up his edit... I also could not find a place to accept the edit... Sorry, still learning. – Reblochon Masque May 10 '14 at 16:04
  • 1
    Don't worry - @Martijn has sufficient rep. to not require edit approvals (he was fixing some of your indentation)... the post's ended up looking fine :) – Jon Clements May 10 '14 at 16:26
0
  1. Why input() in your function?
  2. When you use input(), you get a string but your function requires integers, so you have to apply int() over each element of the input list.


def odd_even(L):
    even=[]
    odd=[]
    for x in L:
        if x%2==0:
            even.append(x)
        else:
            odd.append(x)
    return even, odd


L = input()
L = L.split(" ")
L = list(map(int, L))
print(odd_even(L))
Nicolas
  • 5,583
  • 1
  • 25
  • 37
0

Same but different using a conditional expression:

def is_even(n):
    return not n % 2

def split_odd_even(L):
    '''Split a list of numbers into odds and evens.

    L --> list
    returns tuple of two lists
    '''
    odd, even = list(), list()
    for n in L:
        even.append(n) if is_even(n) else odd.append(n)
    return odd, even

print split_odd_even(range(20))
>>> ([1, 3, 5, 7, 9, 11, 13, 15, 17, 19], [0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
wwii
  • 23,232
  • 7
  • 37
  • 77