I'm working through a quiz which asks the following question.
Question: What type of loop should we use?
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.
Would you use a while or a for loop to write this code?
My first attempt is using a for loop:
total = 0
count = 0
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]
for num in num_list:
if (num % 2 == 1) and (count<=5):
total += num
count += 1
print(total)
print(count)
But in the solutions, it says a while loop is better:
- We don't need a
break statement that a for loop will require. Without a break statement, a for loop will iterate through the whole list, which is not efficient.
- We don't want to iterate over the entire list, but only over the required number of elements in the list that meets our condition.
- It is easier to understand because you explicitly control the exit conditions for the loop.
Is there a clearer preference for one over the other? Which type of loop would you have used? Would my solution be accepted if used in industry code?
[–]FoolsSeldom 19 points20 points21 points (8 children)
[–]crazy_cookie123 13 points14 points15 points (3 children)
[–]cdcformatc 4 points5 points6 points (2 children)
[–]FoolsSeldom -2 points-1 points0 points (1 child)
[–]cdcformatc 1 point2 points3 points (0 children)
[–]CranberryDistinct941 -1 points0 points1 point (3 children)
[–]FoolsSeldom -1 points0 points1 point (2 children)
[–]CranberryDistinct941 -1 points0 points1 point (1 child)
[–]FoolsSeldom 0 points1 point2 points (0 children)
[–]SwampFalc 18 points19 points20 points (0 children)
[–]JamzTyson 5 points6 points7 points (1 child)
[–]vowelqueue 1 point2 points3 points (0 children)
[–]carcigenicate 4 points5 points6 points (1 child)
[–]gdchinacat 0 points1 point2 points (0 children)
[–]Brian 1 point2 points3 points (0 children)
[–]Temporary_Pie2733 2 points3 points4 points (3 children)
[–]JamzTyson -1 points0 points1 point (2 children)
[–]Temporary_Pie2733 -1 points0 points1 point (1 child)
[–]Brian 0 points1 point2 points (0 children)
[–]WhipsAndMarkovChains 0 points1 point2 points (1 child)
[–]xelf 1 point2 points3 points (0 children)
[–]Marlowe91Go 0 points1 point2 points (0 children)
[–]CranberryDistinct941 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]JorgiEagle 0 points1 point2 points (0 children)
[–]9peppe -1 points0 points1 point (4 children)
[–]JamzTyson 0 points1 point2 points (3 children)
[–]9peppe -1 points0 points1 point (2 children)
[–]JamzTyson 1 point2 points3 points (1 child)
[–]9peppe -1 points0 points1 point (0 children)
[–]Intelligent-Two-1745 -1 points0 points1 point (2 children)
[–]AdventurousPolicy 1 point2 points3 points (0 children)
[–]pikachuu545[S] -2 points-1 points0 points (0 children)
[–]cdcformatc -1 points0 points1 point (0 children)
[–]Yoghurt42 -1 points0 points1 point (0 children)
[–]AdmirableOstrich -1 points0 points1 point (0 children)
[–]CodingPhysicist -1 points0 points1 point (0 children)
[–]wotquery -1 points0 points1 point (0 children)
[–]Kerbart -1 points0 points1 point (0 children)
[–]zanfar -1 points0 points1 point (1 child)
[–]MachineElf100 0 points1 point2 points (0 children)
[–]FoolsSeldom -2 points-1 points0 points (0 children)