0
def travels(answer, need, location):
#travel north
    if answer == "north":
        return "you travel north"
#travel south
    elif answer == "south":
        return "you travel south"
#travel east
    elif answer == "east":
        return "You travel east"
#travel west
    elif answer == "west":
        return "You travel west"

basically the function travels is supposed to take in a string (answer) as well as two other variables (which I haven't gotten to yet) and determine a direction that a person travels. However, when I run this I get the response:

Traceback (most recent call last):                                                                                                                                   
  File "cavernousJ.py", line 3, in <module>                                                                                                                          
    from travels import lookAround, findAnswer                                                                                                                       
  File "/home/cg/root/travels.py", line 13                                                                                                                           
    elif answer == "west":                                                                                                                                           
       ^                                                                                                                                                             
SyntaxError: invalid syntax  

1) Where is this syntax error? 2) Why does the terminal respond with an error on the "west" elif and not "south" elif?

All the tutorials I have read seem to indicate that I have used the correct format for the elif statement. Other code where I use a series of elif statements are similar (and not giving me problems). I know it's not the equals sign, and I know I'm supposed to use a : at the end of the elif statement.

user3097236
  • 37
  • 1
  • 8
  • comment need identify region and `def fo(answer,need=None,location=None)` if want calling without additional arguments. – dsgdfg Aug 25 '15 at 18:29
  • 1
    Works for me. How are you calling the method? Are you including dummy args for the other two params? and what about the rest of that class? – Shawn Mehan Aug 25 '15 at 18:31
  • 2
    OP has a tab before `elif answer == "west":`. Click edit and check the raw code. I voted to close for being a typographical error. – Cyphase Aug 25 '15 at 18:33
  • 1
    @cdarke, why is it best to "always" have an `else`? – Cyphase Aug 25 '15 at 18:37
  • Always having an `else` isn't defensive programming, it's just a poor rule. If I had a dollar for every time I saw an `else: pass`, I would have... maybe four dollars or so. Sometimes it's good to have an `else`. Sometimes it isn't. Maybe the OP will determine a poor input by checking for a `return` value of `None`. In this particular case, the `elif`s could even be replaced with `if`s, as each case has a `return` anyway (although something like acushner's suggestion would be superior). – TigerhawkT3 Aug 25 '15 at 18:44
  • @cdarke, yes, I know what defensive programming is :). That's a good point to mention; but there are also cases where it wouldn't make sense to put anything in an `else` except `pass`. – Cyphase Aug 25 '15 at 18:46
  • Possible duplicate of https://stackoverflow.com/questions/38543524/python-if-elif-syntax-error-why – tripleee Oct 30 '19 at 12:44

2 Answers2

5

You have a tab before elif answer == "west":; that's what's causing the error. You also have a tab on the next line. Replace them with spaces. You should also configure your editor to automatically replace tabs with spaces.

Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • You might suggest that editor setting are changed to replace tabs with 4 spaces? – cdarke Aug 25 '15 at 18:36
  • 1
    There's a tab in the line after it, as well. OP should probably use their editor/IDE to automatically replace all tabs with spaces. – TigerhawkT3 Aug 25 '15 at 18:37
3

you could also just do:

if answer.lower() in ('north', 'south', 'east', 'west'):
   return 'you travel ' + answer

unless you really care about the inconsistent capitalization of the word "you"

acushner
  • 9,595
  • 1
  • 34
  • 34