Pybites Logo

Movie theater (refactoring)

Level: Intermediate (score: 3)

Refactor the code from invoice_to_be_refactored inside invoice_refactored and make it more readable. Currently the function exhibits the infamous Arrow anti pattern.

Remember from the Zen of Python: flat is better than nested, so try to flatten it with the following techniques:

1. Adding "guard clauses"

2. Using the "extract method"

To summarize the code:

- If 0 tickets raise a ValueError
- If the movie passed into the function does not match any movies in available_movies raise a LookupError
- For the matching movie get the discount and price:
  - discount = 10 if you buy 5 or more tickets
  - price = 12 if the quality is IMAX else 10
- Calculate and return the price = tickets * price - discount

Note: there is no single correct solution for this exercise. The goal is to get more familiar with refactoring techniques. 

This exercise was inspired by Martin Fowler's Refactoring book.