Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors found in the object_oriented_programming_advanced notebook #172

Open
yakutovicha opened this issue Dec 12, 2023 · 4 comments
Open
Labels
bug Something isn't working

Comments

@yakutovicha
Copy link
Member

No description provided.

@yakutovicha yakutovicha pinned this issue Dec 12, 2023
@despadam
Copy link
Collaborator

Typo in Music Streaming Service exercise: Class User should have attribute username and not name

@edoardob90 edoardob90 changed the title Errors found in the object_oriented_programming_advanced.ipynb notebook Errors found in the object_oriented_programming_advanced notebook Dec 12, 2023
@edoardob90
Copy link
Member

In the "Banking system" exercise, clarify which kind of interest we want to compute. In the tests we are computing a "simple" interest, while someone might attempt to calculate a "compound" interest. We have to make it clear.

@GCatarina
Copy link

GCatarina commented Dec 12, 2023

In "Store Inventory" exercise, solution with 'pop("type")' method does not pass the tests even though it outputs the correct result.

Code example that works but fails the tests:

computers = [
    {
        "type": "PC",
        "name": "pc_1",
        "price": 1500,
        "quantity": 1,
        "expansion_slots": 2
    },
    {
        "type": "Laptop",
        "name": "laptop_1",
        "price": 1200,
        "quantity": 4,
        "battery_life": 6
    }
]

class Computer:
    def __init__(self,name,price,quantity):
        self.name = name
        self.price = price
        self.quantity = quantity
    
    def __str__(self):
        return f"Computer with name '{self.name}', price {self.price} CHF and quantity {self.quantity}."
    
class PC(Computer):
    def __init__(self,name,price,quantity,expansion_slots):
        super().__init__(name,price,quantity)
        self.expansion_slots = expansion_slots
    
    def __str__(self):
        return super().__str__() + f" This PC has {self.expansion_slots} expansion slots."
    
class Laptop(Computer):
    def __init__(self,name,price,quantity,battery_life):
        super().__init__(name,price,quantity)
        self.battery_life = battery_life
    
    def __str__(self):
        return super().__str__() + f" This laptop has a battery life of {self.battery_life} hours."

inventory = []
for computer in computers:
    if computer["type"] == "Computer":
        computer.pop("type")
        inventory.append(Computer(**computer))
    elif computer["type"] == "PC":
        computer.pop("type")
        inventory.append(PC(**computer))
    elif computer["type"] == "Laptop":
        computer.pop("type")
        inventory.append(Laptop(**computer))

sol = []
for it in inventory:
    sol.append(str(it))

print(sol)

@yakutovicha
Copy link
Member Author

yakutovicha commented Dec 12, 2023

In the first exercise, it is not possible to use the define decorator to define Mother and Father classes:

from attrs import define

@define
class Mother:
    eye_color_mother: str

@define
class Father:
    eye_color_father: str

class Child(Mother, Father):
    def __init__(self, eye_color_mother: str, eye_color_father: str):
        Mother.__init__(self, eye_color_mother)
        Father.__init__(self, eye_color_father)
        self.eye_color = self.set_eye_color()

    def set_eye_color(self):
        if self.eye_color_mother == self.eye_color_father:
            return self.eye_color_mother
        return "brown"

An attempt to create a class results in the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 9
      5 @define
      6 class Father:
      7     eye_color_father: str
----> 9 class Child(Mother, Father):
     10     def __init__(self, eye_color_mother: str, eye_color_father: str):
     11         Mother.__init__(self, eye_color_mother)

TypeError: multiple bases have instance lay-out conflict

Replacing the @define decorator with dataclass fixes the issue. But it is still not clear why define is not working.

Some hints from the stackoverflow.

@yakutovicha yakutovicha unpinned this issue Dec 13, 2023
@edoardob90 edoardob90 added the bug Something isn't working label Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants