2017-04-05 183 views
0

蟒蛇很新,但在C有一些經驗。elif語句中python無效的語法

if語句的工作方式與C中的相同嗎?將if語句放在另一個if語句的下面以檢查錯誤返回。

似乎有一個問題,我試圖在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() 
+0

請包括回溯。 – stybl

+1

「elif」之前的行缺少右括號。 –

+0

你在一些'print'語句中有不匹配的括號 – kuro

回答

2

你錯過了上一行括號:

print("{}".format(tweet) 

這應該是:

print("{}".format(tweet)) 

...而且同樣適用於相同print S於行48和53.

+0

非常感謝你! – olafironfoot

2

if/elif/else塊中的最後一個打印語句都丟失了一個clo唱支架

+0

謝謝! – olafironfoot