'function returning a 'none' type instead of string? [duplicate]
I have this function:
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))
now, when I try type(sentiment_review('turtles', 50)), it returns NoneType.
why is the type returned not a string? even when I try return str("there were {} negative tweets and {} positive tweets about {}".format(negative_tweets, positive_tweets, query)), the result is the same.
Solution 1:[1]
print() itself is a function, which doesn't return anything. If you want to return a string, do just that without the 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)
Otherwise, if you want to print, do so without a return
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))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mrd |
