×

Challenges

Challenge 5.1

# Add number of months to a given month

Challenge Level: Growing experience

## Requirement:

Write a program that asks for a month (as a number between 1 and 12) and the number of months to add to the month as the input and displays the result as the name of a month as the output.

Hints
• Make variables called original_month and months_to_add and set their values to the inputs entered by the user. Make a variable called new_month and set its value to the sum of the original_month and months_to_add modulo 12.
• Make a list called months and add the 12 months; January, February, March, April, May, June, July, August, September, October, November and December to your list (list of length 12). Hint: Remember in Python the first item of a list is found at index 0. In this case if you get the answer 0, you need to produce 'January'.
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
10
4
4 months after October is February.

Not yet run ?
12
8
8 months after December is August.

Not yet run ?
1
6
6 months after January is July.

Not yet run ?
6
6
6 months after June is December.

Not yet run ?
12
12
12 months after December is December.

Not yet run ?