Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need python code that collects all even numbers separately, odd numbers separately and displaying into two separate totals. The range of numbers is 1-20

I am new to this language and I keep getting syntax errors, invalid statement errors etc.. So it would be great if you can write a description about the code as well.

Thanks in advance.

Python
even_list = []
odd_list = []
li = range(1,21)
for i in range(len(li)):
    if i/2 == 0 :
        even_list.append(li[i])
    else :
        odd_list.append(li[i])
print even_list
print odd_list


This is what I did but i keep on getting invalid syntax and no output
Posted
Updated 21-Jan-14 23:46pm
v3
Comments
phil.o 22-Jan-14 5:17am    
We do not provide code.
Show us what you have done, we may try to help you. But we will not do your own homework.
Member 10540911 22-Jan-14 5:45am    
even_list = []
odd_list = []
li = range(1,21)
for i in range(len(li)):
if i/2 == 0 :
even_list.append(li[i])
else :
odd_list.append(li[i])
print even_list
print odd_list


This is what I did but keep getting invalid syntax and no output
phil.o 22-Jan-14 5:51am    
On which line is there an invalid syntax reported?
Member 10540911 22-Jan-14 5:53am    
in the line : print even_list

the word print gets highlighted
Member 10540911 22-Jan-14 5:56am    
Now i tried again and the error isn't reported but this i what i get when i press enter:

>>>print even_list
[]
>>>print odd_list
[]

There is no output

1 solution

I'm not a python specialist, but maybe
Python
even_list = []
odd_list = []
li = range(1,21)
for i in li:
    if i%2 == 0 :
        even_list.append(li[i])
    else :
        odd_list.append(li[i])
print even_list
print odd_list


Considerations:
- here you need the elements in li, not their indices; thus for i in li: instead of for i in range(len(li)):
- the function to test whether a number is odd or even is modulo, not division.

Hope this helps. Let us know what this piece of code is giving.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900