Use a while loop to print all numbers in my_list with even values, one number per line. Then, print the odd numbers using a ' for' loop.
my_list = [6, 3, 0, 11, 20, 4, 17]
6
0
20
4
3
11
17
When using GitHub mode, paste your repository URL below and click Save URL to store it. The saved URL will be automatically included with every message you send until you choose to clear it. Learn more
Use a while loop to print all numbers in my_list with even values, one number per line. Then, print the odd numbers using a ' for' loop.
my_list = [6, 3, 0, 11, 20, 4, 17]
6
0
20
4
3
11
17
my_list = [6, 3, 0, 11, 20, 4, 17]
index = 0
while index < len(my_list):
number = my_list[index]
# Even numbers are exactly divisible by 2
if number % 2 == 0:
print(number)
index += 1
my_list = [6, 3, 0, 11, 20, 4, 17]
for number in my_list:
# Odd numbers are not exactly divisible by 2
if number % 2 != 0:
print(number)
Our solutions both rely on using the % operator to determine whether a number is exactly divisible by 2. Even numbers are; odd numbers aren't. In both cases, we only need to compare the result of element % 2 with 0. If number % 2 is 0, the number is even. Otherwise, it is odd.
As with the previous problem, we needed indexing to control the while loop. However, the for loop led to code that is easier to read.
Video Walkthrough
Hi! I'm LSBot. I can help you think through the selected exercise by giving you hints and guidance without revealing the solution. Your code from the editor will be automatically detected. Want to know more? Refer to the LSBot User Guide .
Submit your solution for LSBot review. Hi! I'm LSBot. Your code from the editor will be automatically detected. I'll review your solution and provide feedback to help you improve. Ask questions about your solution or request a comprehensive code review. Want to know more? Refer to the LSBot User Guide .