- To find out if a number is even or odd, use the
%
operator to find the remainder after dividing that number by two. If the remainder is zero the number is even. For example:4 % 2
is 0 and5 % 2
is 1.
Challenge 1.1
Challenge Level: Growing experience
Create a program which asks the user to enter a number and checks if the number is odd or even.
Use the “mod” operation for this challenge.
Your program should display the outputs in the table (shown on the right) for the given inputs provided.
%
operator to find the remainder after dividing that number by two.
If the remainder is zero the number is even.
For example: 4 % 2
is 0 and 5 % 2
is 1.# Print a string directly
print("Hello World!")
# Print a variable
print(my_var)
# Set a variable as a string
fruit_name = "Apple"
# Set a variable as an integer
pieces_of_fruit = 7
# Set a variable from a calculation
cost_of_fruit = pieces_of_fruit * cost_per_item
# Add one to a value
pieces_of_fruit += 1
# Find out the discount on fruit
if pieces_of_fruit > 100:
print("Bulk discount applies")
elif pieces_of_fruit > 5:
print("Discount applies")
else:
print("No discount")
# Print numbers 0-9 - remember Python starts counting from 0
for num in range(10):
print(num)
# Print numbers 0-9 using a while loop and a variable
num = 0
while num < 10:
print(num)
# Increment the variable by one.
# It will prevent an infinite loop!
num += 1
# Create a list of fruit
fruit = ["Apple", "Banana", "Orange", "Pear"]
# Create a function which prints a greeting
def greeting(name):
print("Hello " + name)
# Call the function
greeting("Spiderman")
Enter your code in the editor below