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 made.
Complete the code to transition the order from 'Created' to the next state.
def advance_order(self): if self.state == "Created": self.state = "[1]"
After creation, the order moves to 'Shipped' when it is sent out.
Fix the error in the code that checks if the order can be cancelled only when it is in 'Created' state.
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 before shipping.
Fill both blanks to complete the method that returns True if the order is in a final state.
def is_final_state(self): return self.state == "[1]" or self.state == "[2]"
The final states of an order are 'Cancelled' and 'Delivered' because no further transitions occur after these.
Fill all three blanks to complete the transition method that moves the order from 'Shipped' to 'Delivered' or 'Returned'.
def update_after_shipping(self, returned): if self.state == "[1]": if returned: self.state = "[2]" else: self.state = "[3]"
The order must be in 'Shipped' state to update. If returned is True, state changes to 'Returned', else to 'Delivered'.