Complete the code to define the initial state of the order.
class OrderStateMachine: def __init__(self): self.state = "[1]"
The initial state of an order is usually 'Created' when it is first placed.
Complete the code to add a method that changes the state to 'Shipped'.
def ship_order(self): if self.state == "Created": self.state = "[1]"
When an order is shipped, the state changes from 'Created' to 'Shipped'.
Fix the error in the method that cancels an order only if it is not yet shipped.
def cancel_order(self): if self.state == "[1]": self.state = "Cancelled"
An order can only be cancelled if it is still in the 'Created' state, not after it has been shipped.
Fill both blanks to define a method that marks the order as delivered only if it is shipped.
def deliver_order(self): if self.state == "[1]": self.state = "[2]"
The order can be marked as 'Delivered' only if it is currently 'Shipped'.
Fill both blanks to implement a method that returns True if the order is in a final state (Delivered or Cancelled).
def is_final_state(self): return self.state in ["[1]", "[2]"]
The final states are 'Delivered' and 'Cancelled'.