Studio: Holiday

It is possible to name the days 0 through 6, where day 0 is Sunday and day 6 is Saturday. If you go on a wonderful holiday leaving on day number 3 (a Wednesday) and you return home after 10 nights, you arrive home on day number 6 (a Saturday).

Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the number of day of the week you will return on.

What to think about

  • How many days in week?
  • What happens when we go past the last day in the week?
  • Why start at 0 instead of 1?
  • What mathematic operations could be useful?

Solution with day


In [8]:
days_of_week = [
    # 0         1           2
    'Sunday', 'Monday', 'Tuesday',
    # 3             4           5
    'Wednesday', 'Thursday', 'Friday',
    # 6
    'Saturday',
]

# Gather user input
# Need to use the `int` call so that it'll correctly be 
# an integer for mathmatical operations
leaving_day = int(input('What day are you leaving? '))
length_of_vacation = int(input('How long will you be gone? '))

# Get the day we will hit, it may be larger than 7
final_day = length_of_vacation + leaving_day

# Get the index for the day of the week
# By using the modulo operator we can take advantage of
# knowing the remainder of any numbers.
# For example if the day is 2 and our stay is 5
# Then we will have 7, which means we leave on a Tuesday
# and get back on a Sunday.
final_day_index = final_day % len(days_of_week)

print(days_of_week[final_day_index], final_day_index)


What day are you leaving? 2
How long will you be gone? 10
Friday 5

Solution with only Number


In [9]:
"""
Studio: Holiday
"""

# get input: departure day (0-6) and duration
departure_day = input("Which day are you leaving on? (0=Sun, 1=Mon, etc)")
departure_day = int(departure_day)

duration = input("How many days will you be done?")
duration = int(duration)

# calculate return day and respond
return_day = (departure_day + duration) % 7
print("You will return on day", return_day)


Which day are you leaving on? (0=Sun, 1=Mon, etc)2
How many days will you be done?10
You will return on day 5

Solution provided in class


In [10]:
day = int(input("What day will you leave?"))

travelDays = int(input("How many days will you be gone?"))

newDay = day + travelDays

newDay = newDay % 7
print("You will return on", newDay)


What day will you leave?2
How many days will you be gone?10
You will return on 5

In [ ]: