Hacker News new | past | comments | ask | show | jobs | submit login

except it is not working?

>>> numbers = [1,10,100,1000,10000,100000,1000000] >>> if any(number < 10 for number in numbers):print number

Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> if any(number < 10 for number in numbers):print number NameError: name 'number' is not defined >>> if any(number < 10 for number in numbers): print number

Traceback (most recent call last): File "<pyshell#7>", line 2, in <module> print number NameError: name 'number' is not defined >>>




It is working; the variable "number" is local to the generator expression and can't be used in the print statement. It works if you print something else instead (e.g. "Success!" in the article).

If you just want to print the numbers less than 10, it's easier to do:

  print [number for number in numbers if number < 10]
or

  print '\n'.join(str(number) for number in numbers if number < 10)
if you want the same formatting.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: