Python Forum
Writing python function difficulty
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing python function difficulty
#1
Hi, I need to write a Python function that takes in a list of numbers, and outputs them into two lists, one consisting of even numbers, and the other odd numbers. I have managed to do a simple coding that works but when i tried to move it into a function, it doesn't work. Would appreciate your kind assistance.

import statistics 
import random
 
original_list = [random.randint(1,1000) for x in range(0,100)]
print("Original list:", original_list)

def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return even_numbers
    return odd_numbers
        
print("Odd:", oddandeven(original_list))
print("Even:", oddandeven(original_list))
Reply
#2
The way you implement the function it returns on line 15 and line 16 never get executed. You need to return both even_numbers and odd_numbers simultaneously.

import random
  
original_list = [random.randint(1,1000) for x in range(0,100)]
print("Original list:", original_list)
 
def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return even_numbers, odd_numbers

even, odd = oddandeven(original_list)   
print("Odd: {}".format(odd))
print("Even: {}".format(even))

# you can replace above 3 lines with
print("Even: {}\nOdd: {}".format(*oddandeven(original_list)))
Like this your function would return tuple, which means even numbers will always be first element and odd numbers - second. You may want to return dict instead, which will allow to retrieve respective list by key
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi Buran, thank you for your help. I am trying to figure out how your code works.
Reply
#4
does this help
my_list = [1, 2, 3, 4]
  
def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return even_numbers, odd_numbers

result = oddandeven(my_list)

print(result) # print what your function returns
print(type(result)) # it's tuple
print(result[0]) # use index to access first element in the tuple
print(result[1]) # use index to access second element in the tuple
Output:
([2, 4], [1, 3]) <class 'tuple'> [2, 4] [1, 3]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I am trying to return the even_numbers when i print even_numbers, and return odd_numbers when i print odd_numbers. I think calling the index of the array may not be what my lecturer wants. I will just use your first answer instead. Thanks alot for your help.
Reply
#6
as I said you can return also a dict
my_list = [1, 2, 3, 4]
 
def oddandeven(numbers_list):
    even_numbers = []
    odd_numbers = []    
    for number in numbers_list:
        if number % 2 == 0:
            even_numbers.append(number)
        else: 
            odd_numbers.append(number)
    return {'even':even_numbers, 'odd':odd_numbers}
 
result = oddandeven(my_list)
print('Even numbers: {}'.format(result['even']))
print('Odd numbers: {}'.format(result['odd']))
# instead you can use this:
print('Even numbers: {even}\nOdd numbers: {odd}'.format(**result))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Difficulty when trying to call all the input values extricate 2 1,927 Jun-05-2020, 09:36 AM
Last Post: pyzyx3qwerty
  4 tries choice question, one difficulty extricate 4 2,365 Jun-04-2020, 05:07 AM
Last Post: extricate
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,800 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  is writing a function a pythonic thing to do? Avivlevi815 1 2,112 Dec-03-2018, 06:10 PM
Last Post: Gribouillis
  Increasing difficulty of a maths game Maxxy_Gray 1 3,168 Apr-04-2018, 03:00 PM
Last Post: sparkz_alot
  Writing a function that accepts two integer parameters (lines and cheers) taydeal20 1 3,091 Feb-05-2018, 08:35 PM
Last Post: nilamo
  writing a function for isogram Shazily 4 11,995 Feb-03-2017, 05:35 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020