[BangPypers] Issue with list comprehension

Anant <TECHIE> anant.techie at gmail.com
Wed Oct 30 13:12:36 CET 2013

Hi folks,

I am new to Python (have been on Java for 8 years), but I have quickly
grasped its style.

While working on a problem (as Python practice) to generate fibonacci
numbers using generators and list comprehension, I ran into an issue.

Here's my code:

*import itertools*
*def fibonacci():*
*    a, b = 0, 1*
*    while True:*
* yield b*
* a, b = b, a+b*
*_sum=sum([n for n in itertools.takewhile(lambda x: x < 400, fib)])*
*print _sum*

Basically, I wanted to sum up all the fib numbers till 400.
*This code runs fine when run on REPL (shell). But when I run it*
*as a script, i get following error:*

Traceback (most recent call last):
  File "2.py", line 10, in <module>
    _sum=sum([n for n in itertools.takewhile(lambda x: x < 400, fib)])
TypeError: 'int' object is not callable

 Python version: 2.7.3

Is there a bug in Python or am I missing something? Why does this only work
and not as shell script?

Thanks for help.


More information about the BangPypers mailing list