Now You Code 4: Should I Sleep In?

Always. :-)

Actually we'd like to write a program which will suggest whether or not we can sleep in. Our criteria will be as follows.

  • Is it the weekend? Then sleep in.
  • Is it a holiday? Then sleep in.
  • Otherwise you need to get up!

Let's write a program to input the day of the week and ask whether or not its a holiday, then make the appropriate decision as to whether or not we should sleep in.

We will use the Write - Refactor - Test - Rewrite approach to complete the final solution which will refactor the day of the week logic into a function.

Step 1: Write

Here's the algorithm of this program. Write the solution in Python.

    input day of the week
    input is it a holiday
    if day of the week is a weekend
        print go back to bed its the weekend
    else
        if its a holiday
            print go back to bed. its a holiday
        else
            print you need to get up

Example Run 1:

What day of the week is it? Monday
Is it a holiday? No
You need to get up!

Example Run 2:

What day of the week is it? Saturday
Is it a holiday? No
Go back to bed! It's the weekend.

Example Run 3:

What day of the week is it? Monday
Is it a holiday? Yes
Go back to bed! It's a Holiday.

In [1]:
## TODO: Write the solution here

Step 2: Refactor

Next we need to refactor the part of this program which checks to see if the day of the week is a weekend. The function:

  • IsWeekend() has one input a day_of_week and returns True when it's a Saturday or Sunday, False for all other days.

In [7]:
## Step 2: Refactor into function IsWeekend(day_of_week)

Step 3: Test

Time to test. Write 7 tests, once for each day of the week.

WHEN day_of_week='Monday' We EXPECT IsWeekend(day_of_week) to return False
WHEN day_of_week='Tuesday' We EXPECT IsWeekend(day_of_week) to return False
WHEN day_of_week='Wednesday' We EXPECT IsWeekend(day_of_week) to return False
WHEN day_of_week='Thursday' We EXPECT IsWeekend(day_of_week) to return False
WHEN day_of_week='Friday' We EXPECT IsWeekend(day_of_week) to return False
WHEN day_of_week='Saturday' We EXPECT IsWeekend(day_of_week) to return True
WHEN day_of_week='Sunday' We EXPECT IsWeekend(day_of_week) to return True

In [2]:
# Step 3: Write tests. Similar to how we did it in the lab.

Step 4: rewrite the program to use the function

Finally re-write the original program, but call the function you made in step 2:


In [1]:
## Step 4: Write program here from the algorithm above

Step 5: Questions

  1. Provide 3 advantages for programming a user function in your code like IsWeekend?

Answer:

  1. Why did we need to write 7 tests?

Answer:

  1. Which code is easier to understand? Step 1 or step 4? Why? or Why not?

Answer:

  1. What happens when you input 'sat' for 'Saturday' Does it work? How can the program be improved? Does improving it affect the readability of step 4? (HINT: Abstractions!!!)

Answer:

Step 6: Reflection

Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?

To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.

Keep your response to between 100 and 250 words.

--== Write Your Reflection Below Here ==--