×

Challenges

Challenge 3.3

Add hours on a clock (24 hour clock)

Challenge Level: Growing experience

Requirement:

Write a program that adds hours on a clock; for example, on a 24-hour clock, 11 o'clock plus 3 hours is 14:00. This can be done using the modulo operator.

Hints
• Make variables called original_time and hours_to_add and set their values to the inputs entered by the user. Make a variable called new_time and set its value to the sum of the original_time and hours_to_add modulo 24. Display the output as 00:00 if the value of new_time is equal to 0.
Programming Reminders
# Print a string directly
print("Hello World!")

# Print a variable
print(my_var)

Variables

# 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

Conditionals

# 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")

For loops

# Print numbers 0-9 - remember Python starts counting from 0
for num in range(10):
print(num)

While loops

# 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 

Lists

# Create a list of fruit
fruit = ["Apple", "Banana", "Orange", "Pear"]

Functions

# 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

Your results will be displayed here

Input Expected output Received output Status
11
1
The new time is 12:00.

Not yet run ?
11
4
The new time is 15:00.

Not yet run ?
11
12
The new time is 23:00.

Not yet run ?
22
2
The new time is 00:00.

Not yet run ?
22
4
The new time is 2:00.

Not yet run ?
22
22
The new time is 20:00.

Not yet run ?