Oct-24-2021, 12:56 PM
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.
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!!
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.
1 2 3 4 5 6 7 8 |
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)) |
Is there any better way to re-code the above using function definition / return, e.g.
def add_up_the_odds(numbers):
return
Thanks!!