Python Forum
Use of function/return
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of function/return
#1
Hello all,

I had the task to code the following:

Take a list of integers and returns the value of these numbers added up, but only if they are odd.
- Example input: [1,5,3,2]
- Output: 9

I did the code below and it worked perfectly.

numbers = [1,5,3,2]
print(numbers)
add_up_the_odds = []
for number in numbers:
    if number % 2 == 1:
        add_up_the_odds.append(number)
print(add_up_the_odds)
print(sum(add_up_the_odds))
Now my question is:
Is there any better way to re-code the above using function definition / return, e.g.
def add_up_the_odds(numbers):
return

Thanks!!
Reply
#2
Yes you could code it as a function, give it a go and if you have any problems ask about where you are going wrong.
Paulman likes this post
Reply
#3
I tried with this then:

def add_up_the_odds(numbers):
    odds = []
    for number in range(1,len(numbers)):
      if number % 2 == 1:
          odds.append(number)
    return odds
numbers = [1,5,3,2]
print (sum(odds))
I'm not getting any result, it looks that "odds is not defined", but I defined it at the beginning of the function, what's wrong with this code?
Reply
#4
You didn't call the function add_up_the_odds
Paulman likes this post
Reply
#5
Do you mean the below?

def add_up_the_odds(numbers):
    add_up_the_odds = []
    for number in range(1,len(numbers)):
      if number % 2 == 1:
          add_up_the_odds.append(number)
          return add_up_the_odds
numbers = [1,5,3,2]
print (sum(add_up_the_odds))


It didn't work
Reply
#6
Here is an example of using a function
def a_function(function_input):
    local_variable = "Local Variable"
    return f"{function_input} + {local_variable}"


returned_from_function = a_function("Function Input")

print(returned_from_function)
Output:
Function Input + Local Variable

The code doesn't need to be changed much to turn it into a function
numbers = [1, 5, 3, 2]
print(numbers)


def get_odd_numbers(numbers):
    odd_numbers = []
    for number in numbers:
        if number % 2:
            odd_numbers.append(number)
    return odd_numbers


odd_numbers = get_odd_numbers(numbers)
print(odd_numbers)
print(sum(odd_numbers))
Output:
[1, 5, 3, 2] [1, 5, 3] 9

It can also be written as the more compact
def get_odd_numbers(numbers):
    return [number for number in numbers if number % 2]
Paulman likes this post
Reply
#7
OK many thanks, that's incredibly short, very nice.

In the meantime I managed to do the below, trying to follow your suggestions, to me it looks working fine, much more complicated than your for sure.

def add_up_the_odds(numbers):
    odds = []
    for number in numbers:
      if number % 2 == 1:
          odds.append(number)
    return odds
numbers = [1,5,3,2]
print(numbers)
print(add_up_the_odds(numbers))
print(sum(add_up_the_odds(numbers)))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple return from function Grimmar 7 3,490 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  Lambda function not return value mbilalshafiq 4 3,252 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,180 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,598 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  [split] problem with function return value ops 1 3,279 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 4,828 Jan-28-2020, 06:20 PM
Last Post: snippsat
  return outside function seamus 4 3,021 May-17-2019, 07:38 PM
Last Post: seamus
  Recursive Function - Compare 2 lists, return the elements that don't exist in both KellyBaptist 1 5,181 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Need of return in function if statement inside the function already returns Athul 5 3,868 Aug-16-2018, 10:19 AM
Last Post: DuaneJack
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,168 Jul-01-2018, 01:17 AM
Last Post: juliabrushett

Forum Jump:

User Panel Messages

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