Skip to content
13 changes: 10 additions & 3 deletions checks/classes/classes10.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# checks/classes/classes10.py
assert Circle.pi == 3.14159, "pi should be a class attribute on Circle"
assert abs(c1.area() - 3.14159) < 1e-6, f"area of circle r=1 should be ~3.14159, got {c1.area()}"
assert abs(c2.area() - 3.14159 * 25) < 1e-4
assert c1.pi is c2.pi, "pi should be the same class-level object for all instances"
assert (
abs(c1.area() - 3.14159) < 1e-6
), f"area of circle r=1 should be ~3.14159, got {c1.area()}"
assert (
abs(c2.area() - 3.14159 * 25) < 1e-4
), "area of circle r=5 should be ~78.54 (pi * radius ** 2)"
assert (
c1.pi is c2.pi
), "pi should be the same class-level object for all instances"
print("classes10 ✓")
13 changes: 10 additions & 3 deletions checks/classes/classes11.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
assert isinstance(t, Temperature), "from_string should return a Temperature instance"
# checks/classes/classes11.py
assert isinstance(
t, Temperature
), "from_string should return a Temperature instance"
assert t.degrees == 36.6, f"expected 36.6, got {t.degrees}"
assert boiling.degrees == 100.0
assert isinstance(Temperature.from_string("0C"), Temperature)
assert boiling.degrees == 100.0, (
f"from_string('100C') should give degrees 100.0, " f"got {boiling.degrees}"
)
assert isinstance(
Temperature.from_string("0C"), Temperature
), "from_string('0C') should return a Temperature instance"
print("classes11 ✓")
24 changes: 19 additions & 5 deletions checks/classes/classes12.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
assert account.owner == "Alice", f"owner should be 'Alice', got {account.owner!r}"
assert account.balance == 120, f"balance should be 120 after deposit 50 and withdraw 30, got {account.balance}"
assert str(account) == "BankAccount(Alice, balance=120)", f"got {str(account)!r}"
# checks/classes/classes12.py
assert (
account.owner == "Alice"
), f"owner should be 'Alice', got {account.owner!r}"
assert account.balance == 120, (
"balance should be 120 after deposit 50 and withdraw 30, "
f"got {account.balance}"
)
assert (
str(account) == "BankAccount(Alice, balance=120)"
), f"got {str(account)!r}"

fresh = BankAccount("Bob", 50)
fresh.deposit(25)
assert fresh.balance == 75
assert fresh.balance == 75, (
f"balance should be 75 after opening with 50 and depositing 25, "
f"got {fresh.balance}"
)

import sys

try:
fresh.withdraw(1000)
assert False, "should have raised ValueError"
except ValueError as e:
assert "insufficient" in str(e).lower(), f"ValueError message should mention 'insufficient', got {e!r}"
assert (
"insufficient" in str(e).lower()
), f"ValueError message should mention 'insufficient', got {e!r}"

print("classes12 ✓")
6 changes: 5 additions & 1 deletion checks/classes/classes7.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# checks/classes/classes7.py
assert g.greet() == "Hello, Alice!", f"got {g.greet()!r}"
assert Greeter("Bob").greet() == "Hello, Bob!"
assert Greeter("Bob").greet() == "Hello, Bob!", (
"Greeter('Bob').greet() should return 'Hello, Bob!'; "
"build it from self.get_name()"
)
print("classes7 ✓")
5 changes: 4 additions & 1 deletion checks/classes/classes8.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# checks/classes/classes8.py
assert str(book) == "Book: 1984 by Orwell", f"got {str(book)!r}"
assert str(Book("Dune", "Herbert")) == "Book: Dune by Herbert"
assert (
str(Book("Dune", "Herbert")) == "Book: Dune by Herbert"
), "str(Book('Dune', 'Herbert')) should be 'Book: Dune by Herbert'"
print("classes8 ✓")
11 changes: 9 additions & 2 deletions checks/classes/classes9.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# checks/classes/classes9.py
assert repr(c) == "Color(r=255, g=128, b=0)", f"got {repr(c)!r}"
assert str(c) == "rgb(255, 128, 0)"
assert repr(Color(0, 0, 0)) == "Color(r=0, g=0, b=0)"
assert str(c) == "rgb(255, 128, 0)", (
"str(c) should stay 'rgb(255, 128, 0)'; add __repr__ without "
"changing __str__"
)
assert repr(Color(0, 0, 0)) == "Color(r=0, g=0, b=0)", (
"repr(Color(0, 0, 0)) should be 'Color(r=0, g=0, b=0)'; "
"use the instance values"
)
print("classes9 ✓")
Loading