Reputation: 20004
My code can be found here: https://github.com/thekarangoel/HackerNewsAPI/blob/master/hn.py
The test case is this:
from hn import HN
hn = HN()
for story in hn.get_top_stories()[:10]:
print story.print_story()
print '*' * 50
print ''
This prints my output like so:
.....
.....
.....
**************************************************
Rank: 3
Story ID: 6231993
Title: Pipe Dream? 3D-Printed Model of Hyperloop Created
Link: http://news.yahoo.com/pipe-dream-3d-printed-model-hyperloop-created-192033757.html
Domain: yahoo.com
Points: 1
Submitted by: evo_9
Number of comments: 0
Link to comments: http://news.ycombinator.com/item?id=6231993
None
**************************************************
Rank: 4
Story ID: 6231992
Title: What colour are your bits?
Link: http://ansuz.sooke.bc.ca/entry/23
Domain: bc.ca
Points: 1
Submitted by: grey-area
Number of comments: 0
Link to comments: http://news.ycombinator.com/item?id=6231992
None
.....
.....
Notice the None
at the end of each print? Why is it there? What's wrong with the code?
Upvotes: 1
Views: 4466
Reputation: 368944
Seems likes story.print_story()
does not return anything.
If a function does not return, it returns None
.
>>> def a():
... print 1
...
>>> print a()
1
None
Remove print
from print story.print_story()
.
Upvotes: 3
Reputation: 48599
All function calls in your code are replaced by the function's return value. All functions return None by default. If you have a return statement in your function, then the value specified in the return statement will be returned instead of None.
In this line:
print story.print_story()
the function call:
story.print_story()
is replaced by the return value of print_story(). The print_story() method does not have a return statement in it, so it returns None by default, giving you this:
print None
Upvotes: 0
Reputation: 473803
It looks like your print_story
doesn't return anything. Just replace print story.print_story()
with story.print_story()
.
Here's an example of what is happening:
>>> def print_story():
... print "story"
...
>>> print print_story()
story
None
>>> print_story()
story
Upvotes: 0