

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Classes
incomplete
2: Methods
incomplete
3: Methods Can Return
incomplete
4: Methods vs. Functions
incomplete
5: Constructors
incomplete
6: Constructor Quiz
incomplete
7: Multiple Objects
incomplete
8: Objects Quiz
incomplete
9: Archer Practice
incomplete
10: Class Variables vs. Instance Variables
incomplete
11: Classes Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
If a normal function doesn't return anything, it's typically not a very useful function. In contrast, methods often don't return anything because they can mutate (update) the properties of the object instead. That's exactly what we did in the last assignment.
However, they can return values if you want! They're just functions with access to an object, after all. A common use case is a "getter" method that returns a calculated value based on the properties of the object.
class Soldier:
armor: int = 2
num_weapons: int = 2
def get_speed(self) -> int:
speed = 10
speed -= self.armor
speed -= self.num_weapons
return speed
soldier_one = Soldier()
print(soldier_one.get_speed())
# prints "6"
Building walls in Age of Dragons can be expensive, the larger and stronger the wall, the more it costs.
Complete the get_cost(self) -> int method on the Wall class. It should return the cost of a wall, where the cost is its armor multiplied by its height.