Python Classes & Objects
💡
Exercise 34

Restaurants 10 XP Easy

Ctrl+Enter Run Ctrl+S Save

Welcome to Chapter 7: Classes & Objects! 🏗️

💡 Think of it like this: A class is like a blueprint for building a house. The blueprint describes what a house looks like, but it's not a house itself. When you build a house from that blueprint, that's an object!

# The blueprint (class) class Dog: def __init__(self, name, breed): self.name = name self.breed = breed def bark(self): print(f'{self.name} says: Woof! 🐕') # Creating objects (instances) rex = Dog('Rex', 'Labrador') buddy = Dog('Buddy', 'Golden Retriever') rex.bark() # Rex says: Woof! 🐕 buddy.bark() # Buddy says: Woof! 🐕

Key concepts:

  • class — Creates a blueprint (starts with capital letter!)
  • __init__ — The constructor, runs when you create an object
  • self — Refers to the current object (like "me" in English)
  • Attributes — Variables that belong to an object (e.g., self.name)
  • Methods — Functions that belong to an object (e.g., bark())

In this exercise, you'll create a Restaurant class — think of it as the blueprint for any restaurant! 🍔

📋 Instructions
Create a `Restaurant` class with: - Attributes: `name`, `cuisine`, `rating` - A method `describe()` that prints info about the restaurant Create two restaurant objects and call `describe()` on each: ``` Pizza Palace serves Italian food. Rating: 4.5/5 Sushi House serves Japanese food. Rating: 4.8/5 ```
Use __init__(self, name, cuisine, rating) and def describe(self): with f-string.
⚠️ Try solving it yourself first — you'll learn more!
# The blueprint (class)
class Dog:
    def __init__(self, name, breed):
        self.name = name
        self.breed = breed
    
    def bark(self):
        print(f'{self.name} says: Woof! 🐕')

# Creating objects (instances)
rex = Dog('Rex', 'Labrador')
buddy = Dog('Buddy', 'Golden Retriever')

rex.bark()    # Rex says: Woof! 🐕
buddy.bark()  # Buddy says: Woof! 🐕
🧪 Test Cases
Input
Run your code
Expected
Pizza Palace serves Italian food. Rating: 4.5/5 Sushi House serves Japanese food. Rating: 4.8/5
Expected program output
main.py
Hi! I'm Rex 👋
Output
Ready. Press ▶ Run or Ctrl+Enter.