gpt4 book ai didi

python - 为什么我的代码打印 'None' ?

转载 作者:行者123 更新时间:2023-11-30 23:34:13 25 4
gpt4 key购买 nike

我的代码可以在这里找到:https://github.com/thekarangoel/HackerNewsAPI/blob/master/hn.py

测试用例是这样的:

from hn import HN

hn = HN()

for story in hn.get_top_stories()[:10]:
print story.print_story()
print '*' * 50
print ''

这会打印我的输出,如下所示:

.....
.....
.....
**************************************************

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
.....
.....

注意到每次打印末尾的None了吗?为什么它在那里?代码有什么问题?

最佳答案

似乎 story.print_story() 不会返回任何内容。

如果函数不返回,则返回 None

>>> def a():
... print 1
...
>>> print a()
1
None

printstory.print_story() 中删除 print

关于python - 为什么我的代码打印 'None' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18296830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com