Rullst AcademyRullst Academy/Lesson
Python: from first line to project · Lesson 09 / 12

An error can be a clue

Your program found a word where it expected minutes. Instead of giving up or pretending it worked, let us handle that case.

Learning paths10 min

Let's discover together

  1. 1

    SyntaxError means Python could not parse the structure. ValueError can arise during execution when int receives unsuitable text. Read the type and indicated line; the cause may be earlier.

  2. 2

    try contains the conversion that can fail. except ValueError handles that specific error. Other errors remain visible so unexpected problems are not hidden.

  3. 3

    The example function returns None if conversion fails or the number is not positive. None means no result under this contract; it is not a zero duration.

  4. 4

    Test "15", "abc", "0" and "-2". Do not use except: pass to pretend success. A useful message explains what the user can correct.

✦
YOUR MISSION

Read errors and handle conversion ValueError while keeping failure distinct from success.

Now it's your turn

Discovery workshop

Read, predict, check

Compare the two versions. Show result reveals a prepared output: this panel does not run Python. Then practise with the activity file.
Mini experiment

Learning simulation: compare cards and check your predictions. The browser is not compiling Rust or starting a server.

def parse_minutes(raw):
    try:
        minutes = int(raw)
    except ValueError:
        return None
    if minutes <= 0:
        return None
    return minutes

print(parse_minutes("15"))
Build your answers and check when you are ready.
A moment to understand

Which result distinguishes rejected conversion from a valid duration?

I need a hint

A failure must remain recognisable.

Loading your journey…

Take it with you

Run the four suggested inputs. Change the rule to accept only 1 to 180 minutes and add manual tests for 180 and 181.

PYTHON · .py

Your Python workbench

Here you read and predict prepared outputs. To run the actual code, download the activity file, edit it on your computer and use Python. Rullst Academy grades lesson answers; it does not verify local execution.

Save and run

Extract the activity ZIP into its own folder. Open a terminal there and use your system’s command. If downloading your edited file separately, use its actual name in the command. Save before running again.

Windows
python lesson.py
macOS / Ubuntu
python3 lesson.py
My files and GitHub

The editor keeps a local draft and can send a .py file to GitHub with your confirmation. Download your edited code to preserve it. The ZIP is starter material; for the final project, also publish data and test files following PUBLISH.md.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.