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


Messages In This Thread
Use of function/return - by Paulman - Oct-24-2021, 12:56 PM
RE: Use of function/return - by Yoriz - Oct-24-2021, 01:24 PM
RE: Use of function/return - by Paulman - Oct-24-2021, 09:29 PM
RE: Use of function/return - by Yoriz - Oct-24-2021, 09:30 PM
RE: Use of function/return - by Paulman - Oct-24-2021, 09:40 PM
RE: Use of function/return - by Yoriz - Oct-24-2021, 09:42 PM
RE: Use of function/return - by Paulman - Oct-24-2021, 11:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple return from function Grimmar 7 3,654 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  Lambda function not return value mbilalshafiq 4 3,388 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,275 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,840 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  [split] problem with function return value ops 1 3,409 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 5,023 Jan-28-2020, 06:20 PM
Last Post: snippsat
  return outside function seamus 4 3,114 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,292 Dec-23-2018, 10:10 AM
Last Post: Gribouillis
  Need of return in function if statement inside the function already returns Athul 5 3,980 Aug-16-2018, 10:19 AM
Last Post: DuaneJack
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,258 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