0

I am new to Python app development. I know about the Python exceptions, but when I use the except keyword it shows me SyntaxError. My code is

number = 1
try:
if(number == 1):
except ValueError:
print "yay"
else:

print "sucks"

When I use this code it gives me a syntax error when I add the except keyword. Since I'm new to Python I don't know why it's happening like this. I'm using Python 2.7.

4
  • 1
    There is no block after if. Not that your indentation is at all correct here, but what you are trying to do is entirely impossible. Commented Jun 20, 2014 at 16:16
  • 3
    It is not clear even what you are trying to do. What would throw a ValueError here? Commented Jun 20, 2014 at 16:17
  • 1
    Indent your code correctly Commented Jun 20, 2014 at 16:19
  • @MartijnPieters i have tried another ways too but when i type excpet it shows syntax error ..:/ Commented Jun 20, 2014 at 16:19

3 Answers 3

6
number = 1
try:
    if(number == 1):
        print 'yay'
    else: 
        print 'sucks'
except ValueError:
    print 'Oops, something went wrong'

Follow this structure and you will be golden pony boy.

Sign up to request clarification or add additional context in comments.

7 Comments

..when i type except on python gui ..it shows me syntax error.
Not sure what you mean. I just ran this code and it worked fine. Could you take a screenshot?
postimg.org/image/klnqlswtl see this ..when i type 'yay' it shows me syntax error..
@user3760836: You are using Python 3; print() is a function.
oops ..but when i call except it throws the same error ...is there any special way of calling exception in python 3.4 ??
|
5

You cannot mix up Python statements like that.

try ... except is one compound statement; each of the two blocks contained (for the try and the except) need to be fully independent statements of their own. if ... else ... is also a compound statement, so it has to be entirely within either the try or the except block, or entirely outside of it.

This would work:

number = 1
try:
    if number == 1:
        print("yay")
    else:
        print("sucks")
except ValueError:
    pass

because now the whole of if .. else is inside the try block.

Not that you need to handle ValueError here, there is nothing in the code block that would throw the exception.

From the comments it is clear you are using Python 3 (and IDLE), so you want to use print() as a function.

3 Comments

when i type except on python gui ..it still shows me syntax error
@user3760836: Then either you are doing something wrong, or your Python GUI is showing an error where there is none. Without seeing your exact GUI contents, that is impossible to tell, and way beyond the scope of what we can help you with.
@user3760836: looks like you are seeing this: Syntax error on print with Python 3
0

In Python 3.0 , print became a function, you need to include parenthesis as you must for other functions. So print var becomes print(var)

Since you're using Python3 try the following:

number = 1
try:
    if (number == 1):
         print ("yay")
    else:
        print ("other opts")
except ValueError:
     print ("Something went wrong")

1 Comment

can you take a screenshot? Also can you ensure that indents are specified properly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.