'If statement not wirking, python

 if number == 1:  
    print("One")  
    print("To exit type 'exit'.")

 exit = raw_input()    
    if exit == "exit":       
    print("Welcome back.")    

I am getting error on the if exit == "exit": line... (Using python 2) The full error is : 'unindent does not match any outer indentation level'



Solution 1:[1]

For Python 2.x:

if number == 1:  
    print "One" 
    print "To exit type 'exit'."

choice = raw_input()    
    if choice == "exit":       
        print "Welcome back."

The SyntaxError raised by your code was due to the compound if statement.

It seems you've started a block that is to be indented to inside the if, but it stays at the current indent. Normally, ending an if statement doesn't need an ending indication like } but it does freak out when no code enters the if statement.

If you wanted it to stay the way it was, add an indented pass statement to prevent the error from popping up. It would look like this:

...
if choice == "exit":
    pass    # does absolutely nothing, just acknowledges it
...

Solution 2:[2]

You shouldn't indent the if exit statement. You also need to indent your print("Welcome back.") statement, like so:

if number == 1:
    print("One")  
    print("To exit type 'exit'.")

exit = input()    
if exit == "exit":       
    print("Welcome back.") 

You'll use raw_input() and print 'xyz' in Python 2. Use input() and print('xyz') in Python 3.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GeeTransit
Solution 2