[Tutor] Odd & Even lists

Alan Gauld alan.gauld at yahoo.co.uk
Sat May 30 12:40:04 EDT 2020


On 30/05/2020 15:06, Mary Knauth via Tutor wrote:

> We are passing in a list of numbers. You need to create 2 new lists in your chart, then
> 
> put all odd numbers in one list
> put all even numbers in the other list
> output the odd list first, the even list second
> Current code I am running:

If you search the archives ovver the last two or three months you'll
find this exact problem discussed in detail.

> import sys
> numbers = sys.argv[1].split(',')
> for i in range(0,len(numbers)): # 0 to the length of the list
>   numbers[i]= int(numbers[i]) # creates a list of numbers and converts numberes to an integer
> 
> # Modulo operator to decifer even numbers  
> def isEven(n) :
>   return ((n % 2) == 0) # n % 2 leaves no remainder, therefore even
> 

So far so good, you are on the right track, but...

> for i in range(numbers)

range doesn't take a list of numbers as an argument, it expects a
start,stop, step sequence of integers. I suspect what you really want
here is the simpler

for i in numbers:

>   if i == isEven:
>     def listEven = [i]

But this makes no sense watsoever.

def is for defining functions like isEven above.

I think you need to  do some revision on how to populate a list.
In particular consider the append() method..
> print(listOdd)
> listOdd.append(i)

This needs to happen in the if construct.
You are way too late down here...



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list