Created
May 28, 2012 03:27
-
-
Save imankulov/2817044 to your computer and use it in GitHub Desktop.
Find out why this script doesn't work (discount calculator)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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