這個問題在這里已經有了答案: 為什么列印函式回傳 None? (2 個回答) 2 小時前關閉。
我有這個功能:
def sentiment_review(query, maxresults):
.
.
.
positive_tweets = pds.sqldf("select count(sentiment)as n from df where sentiment =
'POSITIVE'")['n'].iloc[0]
negative_tweets = pds.sqldf("select count(sentiment)as n from df where sentiment =
'NEGATIVE'")['n'].iloc[0]
return print("there were {} negative tweets and {} positive tweets about
{}".format(negative_tweets, positive_tweets, query))
現在,當我嘗試時type(sentiment_review('turtles', 50)),它會回傳NoneType。為什么回傳的型別不是字串?即使我嘗試return str("there were {} negative tweets and {} positive tweets about {}".format(negative_tweets, positive_tweets, query)),結果也是一樣的。
uj5u.com熱心網友回復:
print()本身是一個函式,它不回傳任何東西。如果你想回傳一個字串,只需要不列印:
def sentiment_review(query, maxresults):
...
# return a string, without printing to standard out
return "there were {} negative tweets and {} positive tweets about {}".format(
negative_tweets, positive_tweets, query)
否則,如果您想列印,請不要退貨
def sentiment_review(query, maxresults):
...
# print, no return
# (A function without a return effectively returns None anyway.)
print("there were {} negative tweets and {} positive tweets about {}".format(
negative_tweets, positive_tweets, query))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470717.html
上一篇:我想把一個串列放在一個字串上
