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
5 Answers 5
I see a couple things wrong with your current code and some things that are unessaccary.
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
Comments
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
Comments
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
Comments
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)
Comments
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)
10 Comments
filter(lambda x: x%2 == 1, num_list) would be a lot simpler.(i for i in num_list if i%2 == 1)filter instance to a list first; sum can add up arbitrary iterable values.
resultis wrong. This would be correct:85 + 719 + 85 + 17 + 87