Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Python invalid syntax error

  • 10-06-2013 03:45PM
    #1
    Registered Users, Registered Users 2 Posts: 651 ✭✭✭


    Hi All,

    Writing the code below in Python 3.3 I keep getting an invalid syntax error when I try to run it:

    thing ='house'
    animal ='cat'
    if thing == 'animal':
    if animal == 'cat':
    print('This is a cat')
    else:
    print('I dont know what this animal is')
    else:
    print ('I dont know what this thing is')



    Anyone any ideas as to why this is happening? (Bad copy and paste, the text is not indented but hopefully someone can see where I have messed up)

    Much appreciated,

    Nika


Comments

  • Registered Users, Registered Users 2 Posts: 651 ✭✭✭Nika Bolokov


    thing = 'animal'
    animal = 'cat'

    if thing == 'animal':
    if animal == 'cat':
    print ('This is a cat')
    else:
    print ('I dont know what this animal is')

    else:
    print('I dont know what this thing is')


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    The indentation is important in python, leaving it out is not optional.

    Put code tags around your post.


  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    The code looks fine, but the indentation does matter. This is what it should look like:
    thing ='house'
    animal ='cat'
    if thing == 'animal':
        if animal == 'cat':
            print('This is a cat')
        else:
            print('I dont know what this animal is')
    else:
        print ('I dont know what this thing is')
    


  • Registered Users, Registered Users 2 Posts: 651 ✭✭✭Nika Bolokov


    Thanks so much guys. I had the text indented but the else command was not down properly.

    Cheers !


  • Registered Users, Registered Users 2 Posts: 4,182 ✭✭✭spaceHopper


    What editor are you using, I find pyscripter great as it pick up trouple with indenting and syntax you can also use the interpiter free from to try out cose


  • Advertisement
Advertisement