python中的elif语句无效语法 [英] elif statement in python invalid syntax

查看:215
本文介绍了python中的elif语句无效语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与python相当新,但是对C有一定的经验.

Pretty new with python, but have some experience in C.

if语句的工作方式与C中的工作方式相同吗?将if语句放在另一个if语句下面,以检查错误的返回值.

Does the if statements work the same way as in C? Putting an if statement below another if statement to check for false return.

尝试在python中使用elif语句似乎出现了问题,有人可以帮我弄清楚问题出在哪里吗?

There seem to be an issue with me trying to use an elif statement in python can some one help me figure out what is the issue here?

#!/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 也是如此.

... And the same goes for the identical prints on lines 48 and 53.

这篇关于python中的elif语句无效语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆