Python Forum
Writing python function difficulty
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing python function difficulty
#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


Messages In This Thread
Writing python function difficulty - by kirito85 - Oct-26-2018, 01:48 PM
RE: Writing python function difficulty - by buran - Oct-26-2018, 02:17 PM
RE: Writing python function difficulty - by buran - Oct-27-2018, 07:31 AM
RE: Writing python function difficulty - by buran - Oct-28-2018, 07:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Difficulty when trying to call all the input values extricate 2 1,979 Jun-05-2020, 09:36 AM
Last Post: pyzyx3qwerty
  4 tries choice question, one difficulty extricate 4 2,429 Jun-04-2020, 05:07 AM
Last Post: extricate
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,878 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  is writing a function a pythonic thing to do? Avivlevi815 1 2,156 Dec-03-2018, 06:10 PM
Last Post: Gribouillis
  Increasing difficulty of a maths game Maxxy_Gray 1 3,225 Apr-04-2018, 03:00 PM
Last Post: sparkz_alot
  Writing a function that accepts two integer parameters (lines and cheers) taydeal20 1 3,138 Feb-05-2018, 08:35 PM
Last Post: nilamo
  writing a function for isogram Shazily 4 12,083 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