Lesson Finder (Python Challenge)

In this project, you’ll design a Python program that stores your school timetable (e.g. 5 lessons per day, Monday to Friday) and allows you to quickly find out what lesson you have on any given day and period. Simply enter a day (e.g. Tuesday) and a lesson number (e.g. 2), and the program will instantly tell you what subject you have, who the teacher is, and where the class is held.

Example Output:

“On Tuesday, Lesson 2 you have a Maths lesson with Mr. Smith in Room 105.”

Challenge Objectives

By completing this project, you will gain experience with:

  • Data Structures: Using dictionaries and lists to store and organise your timetable.
  • Input Validation: Taking input from the user and applying validation checks on the data being entered.
  • Output Formatting: Creating a user-friendly messages for the user to provide them with the required information.

Step 1: Storing the timetable using dictionaries and lists in Python

To complete this challenge, our first step will be to store our timetable within the code of our program. We will do so by using a combination of dictionaries and lists in Python.

  • Dictionaries ({}) are used to store data as key-value pairs. For example, in the Lesson Finder, each day of the week (e.g., “Monday”) is a key, and its value is a list of lessons for that day.
  • Lists ([]) are used to store ordered collections of items. In this project, each day’s lessons are stored as a list of dictionaries, where each dictionary contains details like the subject, teacher, and room.

To store the 5-day timetable so we will use a dictionary: Each key will represent a day, and the value will be a list of lessons for that day:

timetable = {
    "Monday": [
        {"subject": "English", "teacher": "TS", "room": "201"},
        {"subject": "Maths", "teacher": "MX", "room": "105"},
        {"subject": "Biology", "teacher": "SC", "room": "302"},
        {"subject": "History", "teacher": "HT", "room": "110"},
        {"subject": "Art", "teacher": "AR", "room": "403"}
    ],
    "Tuesday": [
        {"subject": "Maths", "teacher": "MX", "room": "105"},
        {"subject": "Physics", "teacher": "PH", "room": "301"},
        {"subject": "English", "teacher": "TS", "room": "201"},
        {"subject": "PE", "teacher": "PB", "room": "Gym"},
        {"subject": "ICT", "teacher": "KN", "room": "205"}
    ],
    # Add the rest of your days here
}

JSON Syntax?

If you are familiar with JSON syntax, you may have noticed that the syntax of our timetable built using a mix of dictionaries and lists in Python mirrors the JSON syntax (JavaScript Object Notation). JSON is a lightweight data format that is easy for humans to read and write, and easy for machines to parse and generate. It’s widely used for storing and exchanging data, especially in web applications. In Python, using a combination of dictionaries and lists allows us to create a structured, nested format that is both flexible and easy to work with—just like JSON!

Step 2: Receiving and validating user inputs

Now that we have stored our timetable in our Python code, we can retrieve the two inputs from the user: The day of the Week (Monday to Friday) and the period (1 to 5) of the lesson.

For our first input, we will use a look-Up check to validate the day of the week as we will only accept the following values: Monday, Tuesday, Wednesday, Thursday, Friday.

day = input("Enter the day of the week: ").title()
while day not in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]:
   print("Invalid input! Please try again.")
   day = input("Enter the day of the week: ").title()

For our second input, we will use a range check to make sure only a value between 1 and 5 is being entered.

lessonNumber = int(input("Enter the lesson number (1-5): "))
while lessonNumber <1 or lessonNumber >5:
   print("Invalid input! Please try again.")
   lessonNumber = int(input("Enter the lesson number (1-5): "))

Step 3: Extracting the required information from the timetable

The following code enables us to access the different values from our timetable:

lesson = timetable[day][lessonNumber - 1]
subject = lesson["subject"]
teacher = lesson["teacher"]
room = lesson["room"]

We can then output a user-friendly message to the end-user using string concatenation:

print("On " + day + ", lesson " + str(lessonNumber) + ", you have a " + subject + " lesson with " + teacher + " in room " + room + ".")

Python Code

Here is the Python code for this challenge. You can tweak this code to enter your own timetable:

Your Challenge

Can you tweak this code to start by generating a printout of the whole timetable. The output would look like this:

>>> Weekly Timetable <<<

>>> Monday
> Lesson 1: English with TS in room 201.
> Lesson 2: Maths with MX in room 105.
> Lesson 3: Biology with SC in room 302.
> Lesson 4: History with HT in room 110.
> Lesson 5: Art with AR in room 403.

>>> Tuesday
> Lesson 1: Maths with MX in room 105.
> Lesson 2: Physics with PH in room 301.
> Lesson 3: English with TS in room 201.
> Lesson 4: PE with PB in room Gym.
> Lesson 5: ICT with KN in room 205.
...
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.7 / 5. Vote count: 3

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!