avatar Bite 363. Movie Theater (Refactoring)

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.
Login and get coding
go back Intermediate level
Bitecoin 3X

20 out of 22 users completed this Bite.
Will you be Pythonista #21 to crack this Bite?
Resolution time: ~23 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 2.0 on a 1-10 difficulty scale.
» You can do it! 😌

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help