Adding odd numbers in a list

前端 未结 6 971
执笔经年
执笔经年 2021-01-01 03:37

I am trying to find the sum of all odd numbers within a given range but I cannot figure out how to specify which numbers are odd. My professor said to use \"for num in numbe

相关标签:
6条回答
  • 2021-01-01 04:12

    You were nearly there; using num % 2 is the correct method to test for odd and even numbers.

    return exits a function the moment it is executed. Your function returns when the first odd number is encountered.

    Don't use sum() if you use a loop, just add the numbers directly:

    def addOddNumbers(numbers):
        total = 0
        for num in numbers:
            if num % 2 == 1:
                total += num
        print total
    

    You could first build a list of odd numbers for sum():

    def addOddNumbers(numbers):
        odd = []
        for num in numbers:
            if num % 2 == 1:
                odd.append(num)
        print sum(odd)
    

    For sum(), you can use a generator expression:

    def addOddNumbers(numbers):
        print sum(num for num in numbers if num % 2 == 1)
    
    0 讨论(0)
  • 2021-01-01 04:14

    Just to make a quick comment in the form of an answer. If you wanted to make a list of all the odd number in a between 0 and 10, the end points don't matter. If the list was from 0 to 11 then the end point matters. Just remember that range(0,11) = [0,1,2,3,4,5,6,7,8,9,10] and won't include 11.


    Now to explain the generator that is being used. To make to list using numbers as defined by you, you could do this.

    odds = []
    for num in numbers:
        if num % 2 == 1:
            odds.append(num)
    

    This would give you odds = [1,3,5,7,9]. Python has something call list comprehension that make it really easy to do things like this. The above can be easily written like this

    odds = [num for num in number if num % 2 == 1]
    

    and since you want the sum off all the numbers in the list and the sum function takes list, the answer is just

    sum([num for num in number if num % 2 == 1])
    

    Note that most of the answer don't have brackets. This is because without it it becomes a generator.

    odds = (num for num in number if num % 2 == )
    print odds  #--> <generator object <genexpr> at 0x7f9d220669b0>
    odds.next() # 1
    odds.next() # 3
    ...
    

    Since sum takes these as well and generator are faster and less memory intensive than list that is why the best answer is

    sum(num for num in numbers if num % 2 == 1)
    
    0 讨论(0)
  • 2021-01-01 04:14

    The modulo operator "%" is what you want, and have used. However, modulo returns the remainder, so the remainder of an even number divided by 2 is 0.

    So, you want to change:

    if num % 2 == 1:
    

    to

    if num % 2 == 0:
    
    0 讨论(0)
  • 2021-01-01 04:21

    I believe sum(range(1,end_number,2) will work well, and easy,.

    0 讨论(0)
  • 2021-01-01 04:29

    May I suggest a small tweak to Martijn Pieter's excellent answer?

    To make your method more flexible, try incorporating range into the method. It will make your method able to sum the odd values in any list. Also, I switched up your method signature so that it conforms to the Python Style Guide PEP8, just being picky here:)

    def add_odd_numbers(max_list_value):
        numbers = range(0, max_list_value)
        total = 0
        for num in numbers:
            if num % 2 == 1:
                total += num
        return total
    
    if __name__ == '__main__':
        print add_odd_numbers(10)
    
    0 讨论(0)
  • 2021-01-01 04:31

    You could do this:

    def addOddNumbers(numbers):
        return sum(num for num in numbers if num % 2 == 1) # or use print instead of return
    

    To print it if you use return, you would precede the function call with the print statement:

    print addOddNumbers(numbers)
    

    your statement return sum just returns the sum function(nothing else) and exits the addOddNumbers function.

    print sum(numbers) actually just prints the sum of EVERY number in numbers:

    you for loop if would need a variable to keep track of your total:

    total = 0
    for n in numbers:
        if n % 2 == 1:
            total += n # total accumulates the sum of all odd numbers then you can return or print it
    
    0 讨论(0)
提交回复
热议问题