Final project: station controller
Let’s understand
Build a program from familiar parts. A Station class holds one object’s energy. Its build(cost) method checks whether a construction is affordable. If so, it subtracts the cost and returns BUILT; otherwise it changes nothing and returns WAIT. A negative cost also returns WAIT. Separating the decision from input lets another game reuse the method.
Listen to the explanation
Example
# Break the project into three parts:
# 1. Store energy in an object.
# 2. Make one build decision.
# 3. Read and process all requests.Create Station with __init__(self, energy) and build(self, cost). Read starting energy, a request count n, then n integer costs. Print BUILT or WAIT for each request, then the remaining energy. Allow a build when cost >= 0 and cost <= self.energy.
I need a hint
Test the class with one build, then add the loop. Update self.energy inside the method, not a separate local variable. Test zero cost, insufficient energy and several builds in a row.
How to find an error
- Read the last line of the error message.
- Find the indicated line. Check quotes, parentheses and indentation.
- Change one thing and run again.
main.py
Your draft stays in this browser.
Result
PythonYour program’s output will appear here. Click Run.
Мой профиль