Write a function called is_even
that takes one parameter and returns a boolean value. It should return True
if the argument is even; it should return False
otherwise.
The is_even
function should not print anything out or return a number. It should only take in a number and return a boolean.
Note: Be sure to include comments for all functions that you use or create.
For example, if you made a call like:
is_even_number
should have the value True
.
Once you’ve written this function, write a program that asks the user for integers and prints whether the number they entered is even or odd using your is_even
function. You should let the user keep entering numbers until they enter the SENTINEL value.
Example:
Enter a number: 5
Odd
Enter a number: 42
Even
Enter a number: -6
Even
Enter a number: 0
Done!
Here’s how you can create the `is_even` function and a program to interact with the user:
def is_even(number):
"""Check if the number is even.
Args:
number (int): The number to check.
Returns:
bool: True if the number is even, False otherwise.
"""
return number % 2 == 0 # Returns True if the number is divisible by 2
# Sentinel value to stop the program
SENTINEL = 0
while True:
user_input = int(input("Enter a number: ")) # Ask the user for input
if user_input == SENTINEL: # Check if the input is the sentinel value
print("Done!")
break # Exit the loop
# Use the is_even function to check the number
if is_even(user_input):
print("Even")
else:
print("Odd")
### Explanation:
– Function Definition: The `is_even` function takes one parameter (an integer) and returns `True` if the number is even (i.e., divisible by 2 without a remainder) and `False` otherwise.
– User Input Loop: The program uses a `while` loop to continuously prompt the user for input until the sentinel value (0) is entered. It checks if the input number is even or odd using the `is_even` function and prints the corresponding
Here’s how you can write the `is_even` function and the program to check if a number is even or odd based on user input.
def is_even(number):
"""
Check if a number is even.
Parameters:
number (int): The number to check.
Returns:
bool: True if the number is even, False otherwise.
"""
return number % 2 == 0 # Returns True if number is even, False if odd
# Sentinel value to end the input loop
SENTINEL = 'done'
# Start a loop to get user input
while True:
user_input = input("Enter a number (or type 'done' to exit): ")
if user_input.lower() == SENTINEL:
print("Done!") # Print a message when user enters the sentinel value
break # Exit the loop
try:
number = int(user_input) # Convert input to an integer
if is_even(number): # Check if the number is even
print("Even")
else:
print("Odd") # If it's not even, it's odd
except ValueError:
print("Please enter a valid integer.") # Handle invalid input
### Explanation:
1. is_even Function: This function takes one parameter (a number) and uses the modulo operator `%` to check if the number is even
# Function to check if a number is even
def is_even(number):
“””
This function takes an integer as input and returns True if the number is even,
otherwise it returns False.
“””
return number % 2 == 0
# Main program
print(“Enter numbers to check if they are even or odd. Enter 0 to stop.”)
while True:
# Input from the user
num = int(input(“Enter a number: “))
# Sentinel value to break the loop
if num == 0:
print(“Done!”)
break
# Use is_even function to check and print result
if is_even(num):
print(“Even”)
else:
print(“Odd”)