// Data Structures
Lists Mini Project — Even Numbers
What you're building
A program that takes 6 numbers from the user, stores them in a list, and returns a new list with only the even ones.
The filtering happens inside a function.
What you need to know first
- Lists — creating and appending
forloopsinput()andint()- The modulo operator
% - Functions — defining, parameters, return
The brief
Build a program that:
- Asks the user to enter 6 numbers one at a time
- Stores them in a list
- Passes the list to a function called
get_even() - The function returns a new list with only the even numbers
- Prints both lists — the original and the filtered one
Think before you code
- How do you collect 6 inputs and store them in a list?
- How do you check if a number is even?
- How do you build a new list inside a function and return it?
- What should the function return if no numbers are even?
Your starting point
def get_even(numbers):
# your code here
pass
numbers = []
for i in range(6):
num = int(input(f"Number {i + 1}: "))
numbers.append(num)
even = get_even(numbers)
print(f"\nAll numbers: {numbers}")
print(f"Even numbers: {even}")
Expected output
Number 1: 4
Number 2: 7
Number 3: 12
Number 4: 3
Number 5: 8
Number 6: 15
All numbers: [4, 7, 12, 3, 8, 15]
Even numbers: [4, 12, 8]
Heads up!
- A number is even if
number % 2 == 0 - Build a new list inside the function — don't modify the original
- If no numbers are even, the function returns an empty list — that's valid
- The original list should be unchanged after the function runs
The solution
def get_even(numbers):
even = []
for num in numbers:
if num % 2 == 0:
even.append(num)
return even
numbers = []
for i in range(6):
num = int(input(f"Number {i + 1}: "))
numbers.append(num)
even = get_even(numbers)
print(f"\nAll numbers: {numbers}")
print(f"Even numbers: {even}")
// resources
// 0 comments
// No comments yet. Be the first.
// leave a comment