Skip to content

Instantly share code, notes, and snippets.

@imankulov
Created May 28, 2012 03:27
Show Gist options
  • Save imankulov/2817044 to your computer and use it in GitHub Desktop.
Save imankulov/2817044 to your computer and use it in GitHub Desktop.
Find out why this script doesn't work (discount calculator)
# -*- coding: utf-8 -*-
"""
Why calculator doesn't work and how to fix it?
I expect to see:
$ python /tmp/discount_calculator.py
950.0
Instead I've got:
$ python /tmp/discount_calculator.py
Traceback (most recent call last):
File "/tmp/discount_calculator.py", line 18, in <module>
print calculator(5)
File "/tmp/discount_calculator.py", line 11, in _calculator
base_price *= (100 - discount_in_percent) / 100.0
UnboundLocalError: local variable 'base_price' referenced before assignment
"""
def discount_calculator(base_price):
def _calculator(discount_in_percent):
base_price *= (100 - discount_in_percent) / 100.0
return base_price
return _calculator
if __name__ == '__main__':
calculator = discount_calculator(1000)
print calculator(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment