python 中的 elif 语句语法无效
elif statement in python invalid syntax
对 python 很陌生,但对 C 有一些经验。
if 语句的工作方式是否与 C 中的相同?将一个 if 语句放在另一个 if 语句下面以检查 false return。
我在 python 中尝试使用 elif 语句时似乎遇到了问题 有人可以帮我弄清楚这里的问题是什么吗?
#!/usr/bin/env python3
# TODO
#import nltk
from helpers import get_user_timeline
from analyzer import Analyzer
#ZYMAYLA'S HINTS
#ensure proper usage
#argv
def main():
if len(sys.argv) != 2:
sys.exit("Usage: ./smile @username")
# absolute paths to lists
positives = os.path.join(sys.path[0], "positive-words.txt")
negatives = os.path.join(sys.path[0], "negative-words.txt")
#get tweets
##get_user_timeline (in helpers.py)
if get_user_timeline(screen_name, count=200) is False:
#Check if successful
#if private account or does not exist(unsuccessful)
#error message if unsuccessful(sys.exit)
sys.exit("account either private or does not exist")
#tokenize the tweet (like we did in analyzer.py for "smile")
#tokenizers are part of natural language toolkit
#use a TweetTokenizer to split into a list of words
#analyze tweets
#initialize Analyzer
analyzer = Analyzer(positives, negatives)
#instantiate Analyzer, iterate over every token scoring them pos,neg,neutral (this will indicate if the tweet is posistive/negative/neutral)
score = analyzer.TweetAnalyzer(sys.argv[1])
if score > 0.0:
#print score
print(colored("{}".format(score), "green", end=''))
#print tweet
print("{}".format(tweet)
elif score < 0.0:
print(colored("{}".format(score), "red", end=''))
#print tweet
print("{}".format(tweet)
else:
print(colored("{}".format(score), "yellow", end=''))
#print tweet
print("{}".format(tweet)
if __name__ == "__main__":
main()
您在上一行中缺少括号:
print("{}".format(tweet)
这应该是:
print("{}".format(tweet))
...第 48 行和第 53 行相同的 print
s 也是如此。
你的 if/elif/else 块中的最后一个打印语句都缺少右括号
对 python 很陌生,但对 C 有一些经验。
if 语句的工作方式是否与 C 中的相同?将一个 if 语句放在另一个 if 语句下面以检查 false return。
我在 python 中尝试使用 elif 语句时似乎遇到了问题 有人可以帮我弄清楚这里的问题是什么吗?
#!/usr/bin/env python3
# TODO
#import nltk
from helpers import get_user_timeline
from analyzer import Analyzer
#ZYMAYLA'S HINTS
#ensure proper usage
#argv
def main():
if len(sys.argv) != 2:
sys.exit("Usage: ./smile @username")
# absolute paths to lists
positives = os.path.join(sys.path[0], "positive-words.txt")
negatives = os.path.join(sys.path[0], "negative-words.txt")
#get tweets
##get_user_timeline (in helpers.py)
if get_user_timeline(screen_name, count=200) is False:
#Check if successful
#if private account or does not exist(unsuccessful)
#error message if unsuccessful(sys.exit)
sys.exit("account either private or does not exist")
#tokenize the tweet (like we did in analyzer.py for "smile")
#tokenizers are part of natural language toolkit
#use a TweetTokenizer to split into a list of words
#analyze tweets
#initialize Analyzer
analyzer = Analyzer(positives, negatives)
#instantiate Analyzer, iterate over every token scoring them pos,neg,neutral (this will indicate if the tweet is posistive/negative/neutral)
score = analyzer.TweetAnalyzer(sys.argv[1])
if score > 0.0:
#print score
print(colored("{}".format(score), "green", end=''))
#print tweet
print("{}".format(tweet)
elif score < 0.0:
print(colored("{}".format(score), "red", end=''))
#print tweet
print("{}".format(tweet)
else:
print(colored("{}".format(score), "yellow", end=''))
#print tweet
print("{}".format(tweet)
if __name__ == "__main__":
main()
您在上一行中缺少括号:
print("{}".format(tweet)
这应该是:
print("{}".format(tweet))
...第 48 行和第 53 行相同的 print
s 也是如此。
你的 if/elif/else 块中的最后一个打印语句都缺少右括号