Else Python Syntax Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or python if else posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss python invalid syntax error Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes python syntax error print a minute: Sign up Else Syntax Error Python up vote 0 down vote favorite if len(user_hash) > 0: with open(log_file, "w") as log_f: for name in user_hash: log_f.write("Name:%s \n Email: %s" % (name, email) else len(user_hash) < 0: print "Nothing python syntax error colon happened :(" I keep getting a syntax error on the else statement and I am unsure why it keeps producing this error. I don't have any other else statements in the same def and it still gives error. What do I do? python if-statement share|improve this question edited May 10 '13 at 3:19 BrenBarn 109k11165201 asked May 10 '13 at 3:13 user2364549 2715 You need to indent the code. –Linuxios May 10 '13 at 3:15 2 Please read
Python Syntax Error Checker
the formatting help to get an understanding of how to format code in your posts. Don't insert all those
tags. –BrenBarn May 10 '13 at 3:17 add a comment| 5 Answers 5 active oldest votes up vote 2 down vote accepted Your log_f.write statement is also missing a trailing ')', which is likely confusing the parser...and the indentation doesn't look right. Cut and paste problem? share|improve this answer answered May 10 '13 at 3:15 eluke66 611 The question is on else.. accepted answer, parenthesis.. Weird but True.. :) –pradyunsg May 10 '13 at 4:00 add a comment| up vote 5 down vote In Python, the else statement takes no conditions: if condition: do_1() else: do_else() In your case, since you want to evaluate another condition, after the if, use an elif: if condition1: do_1() elif condition2: do_2() ... # you can have as many elifs as you want else: do_else() Note: Read the docs. share|improve this answer answered May 10 '13 at 3:22 pradyunsg 4,50111245 add a comment| up vote 1 down vote You can't do else like that in Python. You should do (assuming your tabs are correct in real life): elif len(user_hash) < 0: and as mentioned, you're missing a close paren. share|improve this answer answered May 10 '13 at 3:15 schilippe 713 As written, his else belongs to the for, and a for statement can't take an elif. That looks like it might be
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about
Python Syntax Error For Loop
Stack Overflow the company Business Learn more about hiring developers or posting ads python syntax error bad input with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow python syntax error near unexpected token (' is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up 'else' Invalid syntax python [closed] up vote -3 down vote favorite File "proyectil.py", http://stackoverflow.com/questions/16474599/else-syntax-error-python line 23, in
thread ] [ subject ] [ author ] Hi All-- Nigel Pauli wrote: > > I am having the strangest problem here. > I am completely new to python (and programming, really) and am happily > working my way through the van Laningham book. In the spirit of learning > https://mail.python.org/pipermail/tutor/2000-June/001591.html by doing rather than just reading I have been typing in his excellent > screen shots [could be a tad larger for easier reading, mind you] I'll say. I'm looking into redoing them for the second printing; failing that, I will try to put larger versions on the book's website. > and I've > come unstuck on the ones to do with else and elif. Here's what I see in my > *Python Shell* window when working through fig 4.8 which is an amusing syntax error > little number that does a rocket countdown leading to Blastoff! > > >>> for i in range(10,-1,-1): > if i != 0: > print i > else: > > SyntaxError: invalid syntax > >>> > > What should continue after else is... > print "Blastoff!" > > What it looks like is that the part of python that handles else and elif > is missing - but that's daft, isn't it? Is it significant that that else: > below print i isn't hanging indented python syntax error to be back underneath the if 2 lines > above? > > If it helps, I'm using Python 1.5.2 on Mandrake Linux 7.0. > > Many thanks to anyone who can penetrate the fog for me on this one. > Here's a transcript of an interactive session: 25 [/c/temp/PyXML-0.5.4/extensions][183] python Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> for i in range(10,-1,-1): ... if i!=0: ... print i ... else: ... print "Blastoff" ... 10 9 8 7 6 5 4 3 2 1 Blastoff >>> Notice that while Python obligingly lets you know that you need indent levels by printing the "..." prompt, it does not guess at what level you want to use (it can't, otherwise it would be too smart to live). In the example above, I simply used one space for each indent level. "for i..." is at 0; "if i!=0:" is at one, "print i" is at two, and the "else:" line *must* match the indent level for "if i!=0:", so it has to be one. Use the backspace key. "print Blastoff" line is back to indent level two. Hope this helps, Ivan ---------------------------------------------- Ivan Van Laningham Axent Technologies, Inc. http://www.pauahtun.org http://www.foretec.com/python/workshops/1998-11/proceedings.html Army Signal Corps: Cu Chi, Class of '70 Author: Teach Yourself Python in 24 Hours Previous message: [Tutor] else syntax error problem Next message: [Tutor] else syntax error problem Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]