Hey guys again, well i got another question. When ever i have an if statement asking for input and i try to use an elif statement and run the code, it say's 'elif' is an syntax error. Is it an indention problem or something else? Thanks

print "1) Play"

if choice == 1:
    ways = ['L','R','S']
    ways = raw_input("Which way do you want to go(L,R,S)?: ")

    elif direction == 'L':
        print "You have gone left."

Dani AI

Generated

Short answer: the SyntaxError on elif is usually an indentation problem — elif and else must line up with their opening if, not be nested inside that if block. correctly pointed this out. Two other common issues seen in the thread are mixed tabs/spaces (which can produce confusing indentation errors) and mismatched input types/case: raw_input() returns a string, so comparing it to an integer (for example if choice == 1) will never match unless converted, and normalizing input with .upper() or .lower() must be paired with comparisons using the same case.

A minimal, different example showing correct structure and input normalization:

play = raw_input("Play (y/n)? ").strip().lower()

if play == 'y':
    move = raw_input("Choose left/right/straight: ").strip().lower()
    if move == 'left':
        print "Went left."
    elif move == 'right':
        print "Went right."
    else:
        print "Went straight."
else:
    print "Exit."

Troubleshooting tips: keep a consistent indentation style (PEP 8 recommends 4 spaces), configure the editor to convert tabs to spaces, and enable visible whitespace so tabs are obvious. Use .strip() to remove stray spaces and .lower()/.upper() to normalize case before comparisons — in the thread direction = raw_input(...).upper() should be compared against uppercase literals (or change the input normalization to match the checked strings). To verify what the program actually received, print the raw representation with print repr(var) (Python 2) to reveal hidden whitespace or unexpected characters. When numeric comparison is intended, convert the input with int() inside a try/except block.

Checklist:

  • Align elif/else with the original if.
  • Avoid mixing tabs and spaces.
  • Normalize and strip input before comparing.
  • Match types (string vs int) or convert safely.

Recommended Answers

All 4 Replies

Yes, it's indention problem.

print "1) Play"

if choice == 1:
    ways = ['L','R','S']
    ways = raw_input("Which way do you want to go(L,R,S)?: ")

elif direction == 'L':
        print "You have gone left."

And one more question, here in this code:

print "Your choices are: "
print "1) Play"
choice = raw_input("Choose your option(1) ")

loop = 1

if choice == '1':
   dir_list = ['N','S','E','W']
   print "Your on an adventure to find the treasure!"
   print "You will go throughout many places in search of the treasure, Good luck!"
direction = raw_input("Which direction do you want to go(N,S,E,W)?: ").upper()

if direction == 'N':
    way_list = ['Left','Right','Straight']
    print "-_-_-_-_-_-_-_-_-_-_-_-"
    print "You approach an old house,"
    print "The door is unlocked and you go inside"
    direction = raw_input("Which way do you want to go('Left,Right,Straight)?: ")

elif direction == 'Left':
    print "You have gone left"

Whenever i type 'Left' the programs breaks, any suggestions?

print "Your choices are: "
print "1) Play"
choice = raw_input("Choose your option(1) ")

loop = 1

if choice == '1':
   dir_list = ['N','S','E','W']
   print "Your on an adventure to find the treasure!"
   print "You will go throughout many places in search of the treasure, Good luck!"
direction = raw_input("Which direction do you want to go(N,S,E,W)?: ").upper()

if direction == 'N':
    way_list = ['Left','Right','Straight']
    print "-_-_-_-_-_-_-_-_-_-_-_-"
    print "You approach an old house,"
    print "The door is unlocked and you go inside"
    direction = raw_input("Which way do you want to go('Left,Right,Straight)?: ")

    if  direction == 'Left':
    	print "You have gone left"
    elif direction == 'Right':
    	 print "You have gone right"
    else:
    	 print "You have gone Straight"

You indented "elif" in manner to check the first input

direction = raw_input("Which way do you want to go('Left,Right,Straight)?: ")

Thanks MitkOK

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.