Skip to content

Practice module 5

>Errors, Files and OOP: coding practice

Start with the lesson drills, then combine the ideas in the module labs. Every task has automatic checks, progressive hints, reset and a solution.

Module coding progress9 exercises available

Lesson coding drills

One focused editor exercise for every lesson in this module. (0/5 passed this visit)

You can run every exercise now. Create a free ID to keep passes, code attempts and XP.

Lesson 1: Errors and Exceptions

Not passed yet

Create a try/except statement that tries to convert 'cat' with int(). Catch ValueError and print Not a number.

# Write your code below

Lesson 2: File Handling

Not passed yet

Write Hello file! followed by a newline to demo.txt. Then read the file and print its contents without adding an extra newline.

# Write your code below

Lesson 3: Classes and Objects

Not passed yet

Create a Circle class. Its __init__ method should save a radius. Add a show_radius method that prints Radius: 4 for a circle with radius 4.

# Write your code below

Lesson 4: Inheritance and Polymorphism

Not passed yet

Create a parent class Vehicle with a move method that prints Moving. Create a child class Bike that overrides it to print Pedaling. Create a Bike and call move().

# Write your code below

Lesson 5: Iterators, Generators and Next Steps

Not passed yet

Write a generator function named count_up that yields 1, 2, and 3. Loop over it and print each value on its own line.

# Write your code below

Coding practice: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (0/4 passed this visit)

You can run every exercise now. Create a free ID to keep passes, code attempts and XP.

Lab 1: Divide without crashing

Not passed yet

Divide 900 by people = 0 inside try/except so that instead of a crash it prints People must be more than zero.

total = 900
people = 0
# try the division, catch ZeroDivisionError

Lab 2: Parse a number safely

Not passed yet

The text "abc" should become a number. Convert it inside try/except ValueError; on failure print not a number.

text = "abc"
# int(text) will raise — handle it

Lab 3: A Dog class

Not passed yet

Create class Dog whose __init__ takes name, with method bark() returning " says Woof". Print Dog("Rex").bark().

class Dog:
    def __init__(self, name):
        # store name

    def bark(self):
        # return the sentence

print(Dog("Rex").bark())

Lab 4: Inherit and override

Not passed yet

Animal.speak() returns "...". Make Cat(Animal) override it to return "Meow", then print Cat().speak().

class Animal:
    def speak(self):
        return "..."

class Cat(Animal):
    # override speak

print(Cat().speak())

References & Further Reading

Related entries from PyLabs's own reference library.

  • open() (Open a file so you can read from it or write to it.)
  • isinstance() (Check whether a value is an instance of a type.)
  • iter() (Return an iterator for a collection or other iterable.)