Python programming questions

You need to write a loop that takes the numbers in a given list named num_list:
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]

Your code should add up the odd numbers in the list, but only up to the first 5 odd numbers together. If there are more than 5 odd numbers, you should stop at the fifth. If there are fewer than 5 odd numbers, add all of the odd numbers.

Here is my 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]

num_odd = 0
count_num = 0


for i in range(num_list):
    if num_listi%2 != 0:
        count_num +=1
        while count_num < 5:
            num_odd += num_listi
            print(num_odd)


 

2 Comments
 
Most Helpful

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_count = 0

summation = 0

for i in num_list:

    if i%2 != 0:

        summation += i

        odd_count += 1

    if odd_count == 5:

        break

print(summation)

This is a way of doing it. You're using range() wrong. I see you are trying to iterate through the list by getting the index and then putting that into num_listi but you can't range through a list. Instead, you can do range(len(num_list)) which takes the length of the num_list, then creates a range from 0 to that length number. However, this just adds extra unnecessary code. Instead you can just do for i in num_list to iterate through the elements of the list directly. 

The break command just tells the code to break out of the loop (in this case, a for loop), which stops the loop continuing. You can see that I've run the break command after the odd_count is equal to 5, which is when we want the summation to stop.

Hope that helps

 

Non quia sed illum magnam inventore et. Suscipit sint natus nihil ipsum voluptatem voluptatem repellendus reprehenderit. Ut dolorem hic iste dolore fuga.

Sapiente quo aut cupiditate aspernatur aspernatur dolor at. Iusto rem adipisci qui minima in vitae.

Earum molestiae maxime provident accusamus debitis sed. Et iusto adipisci et ut quaerat in. Ipsum laborum blanditiis et labore. Sapiente atque at distinctio tempore.

Recusandae saepe facilis libero sit non. Quo in vitae ex laboriosam. Vel non odit iure accusamus rerum. Recusandae repellat quasi praesentium quas nam quo ut.

Career Advancement Opportunities

June 2026 Investment Banking

  • Evercore 01 99.4%
  • Moelis & Company 01 98.8%
  • JPMorgan 01 98.2%
  • Guggenheim Partners 01 97.7%
  • Morgan Stanley 07 97.1%

Overall Employee Satisfaction

June 2026 Investment Banking

  • Moelis & Company No 99.4%
  • Morgan Stanley 01 98.8%
  • Evercore 01 98.2%
  • BMO Capital Markets 12 97.6%
  • Banco Santander 01 97.1%

Professional Growth Opportunities

June 2026 Investment Banking

  • Moelis & Company No 99.4%
  • Evercore No 98.8%
  • Morgan Stanley 05 98.2%
  • JPMorgan No 97.7%
  • BMO Capital Markets 12 97.1%

Total Avg Compensation

June 2026 Investment Banking

  • Vice President (14) $434
  • Associates (43) $259
  • 3rd+ Year Analyst (8) $210
  • 2nd Year Analyst (22) $179
  • Intern/Summer Associate (13) $156
  • 1st Year Analyst (75) $151
  • Intern/Summer Analyst (67) $101
notes
16 IB Interviews Notes

“... there’s no excuse to not take advantage of the resources out there available to you. Best value for your $ are the...”

Leaderboard

1
redever's picture
redever
99.2
2
BankonBanking's picture
BankonBanking
99.0
3
kanon's picture
kanon
99.0
4
Secyh62's picture
Secyh62
99.0
5
CompBanker's picture
CompBanker
98.9
6
DrApeman's picture
DrApeman
98.9
7
dosk17's picture
dosk17
98.9
8
Betsy Massar's picture
Betsy Massar
98.9
9
GameTheory's picture
GameTheory
98.9
10
bolo up's picture
bolo up
98.8
success
From 10 rejections to 1 dream investment banking internship

“... I believe it was the single biggest reason why I ended up with an offer...”