0

Please help me in understanding why my code is not working as I want it to be. I cannot match the result when I run the code in my head vs the actual result in the console. It is very simple:

Given List "num_list", I want to take the odd numbers, sum them up and show the result, with the condition that it has to take and sum the first 5 odd numbers from the list.

i.e.

result= 85 + 719 + 85 + 17 + 191

num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
result=0
odd_num=[]
count_odd=0
while count_odd < 5:
 for i in range(len(num_list)):
 if num_list[i]%2!=0:
 odd_num.append(num_list[i])
 count_odd+=1 
 result+=sum(odd_num)
print(result)

The thing is that the while loop does not stop at count_odd < 5.

I am learning the very basics and I want to understand what is going on here.

Thank you

Sid
2,1891 gold badge15 silver badges31 bronze badges
asked May 25, 2021 at 1:02
1
  • 1
    result is wrong. This would be correct: 85 + 719 + 85 + 17 + 87 Commented May 25, 2021 at 1:16

5 Answers 5

3

I see a couple things wrong with your current code and some things that are unessaccary.

  • Your while loop is not needed, because you use a `for range`.
  • You don't want to use a `for range` loop when iterating through a list
  • you can simply just append odd numbers to a list in you loop. Then outside of your loop find the sum

    Below I have fixed these mistakes and cleaned up the code.

    num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
    odd_num=[]
    count_odd=0
    for num in num_list:
     if count_odd == 5:
     break
     elif num%2!=0:
     odd_num.append(num)
     count_odd+=1 
    print(sum(odd_num))
    

    output

    993
    
  • answered May 25, 2021 at 1:17
    Sign up to request clarification or add additional context in comments.

    Comments

    2

    Is it required for you to use a while loop? Otherwise a simple solution would be to add a if condition inside your for loop for example

    num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
    result=0
    odd_num=[]
    count_odd=0
    for i in range(len(num_list)):
     if count_odd < 5:
     if num_list[i]%2!=0:
     print(num_list[i])
     odd_num.append(num_list[i])
     count_odd+=1 
     result+=sum(odd_num)
    print(result)
    

    What is happening in your while loop is that your while loop runs, it runs 5 times, meaning your for loop runs 5 times so essentially the result= 85 + 719 + 85 + 17 + 191 calculation is ran 5 times which is something you dont want. You want to check your count inside your for loop, not have the for loop run 5 times. Hope that makes sense

    answered May 25, 2021 at 1:11

    Comments

    1

    It's because the for loop finishes completely each iteration of the while loop even if 5 odds have been found. You could just check the count at the end of each loop.

    while count_odd < 5:
     for i in range(len(num_list)):
     if num_list[i] % 2 != 0:
     odd_num.append(num_list[i])
     count_odd += 1 
     result+=sum(odd_num)
     if count_odd >= 5:
     break
    

    Or you could just remove the for loop entirely.

    i = 0
    while count_odd < 5:
     if num_list[i] % 2 != 0:
     odd_num.append(num_list[i])
     count_odd += 1 
     result += sum(odd_num)
     print(count_odd)
     i += 1
    
    answered May 25, 2021 at 1:15

    Comments

    1

    You only need one loop, the while loop.

    num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
    odd_num=[]
    count_odd=0
    i = 0
    while count_odd < 5:
     print(num_list[i])
     if num_list[i]%2!=0:
     odd_num.append(num_list[i])
     count_odd+=1 
     i += 1
    result = sum(odd_num)
    print(result)
    
    answered May 25, 2021 at 1:16

    Comments

    0

    You can use a list comprehension to iterate through the list, filter out all the even numbers, use list slicing to select the first 5 odd numbers, then sum them.

    n = sum(list(filter(None, [i if i%2==1 else None for i in num_list]))[:5])
    

    print(n) gives:

    993
    

    or 85 +たす 719 +たす 85 +たす 17 +たす 87 = 993 (the first 5 odd numbers)

    answered May 25, 2021 at 1:14

    10 Comments

    filter(lambda x: x%2 == 1, num_list) would be a lot simpler.
    Or (i for i in num_list if i%2 == 1)
    You also don't need to convert the filter instance to a list first; sum can add up arbitrary iterable values.
    still need to draw it in a sum function sum(list(filter(lambda x: x%2 != 0, num_list))[:5])
    TypeError: 'filter' object is not subscriptable you do need to convert it to a list, how else are you going to use list slicing to select only the first 5 items?
    |

    Your Answer

    Draft saved
    Draft discarded

    Sign up or log in

    Sign up using Google
    Sign up using Email and Password

    Post as a guest

    Required, but never shown

    Post as a guest

    Required, but never shown

    By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.