Take the 2-minute tour ×

I'm trying to figure out a 'simple' dictionary/database with Python, which will retrieve a name from a list of ten, with the requested information. i.e. input is 'John phone'; output is 'John's phone number is 0401'.

Apart from being completely stuck on this retrieval of specific information, python has suddenly been giving me a syntax error on the name = raw_input line.

The following is the file i've saved as 'friends':


friends = {'John' : {'phone' : '0401',
        'birthday' : '31 July',
        'address' : 'UK',
        'interests' : ['a', 'b', 'c']},
    'Harry' : {'phone' : '0402',
        'birthday' : '2 August',
        'address' : 'Hungary',
        'interests' : ['d', 'e', 'f']}}
name = raw_input ('Who are you looking for? ')
if name in friends: 
print friends[name]"'s phone number is: "['phone']
    # this line is something I haven't tested
    # and so I have a big hunch it's not going to work
    print 'no data'

and this is the error it's giving me:

$ ./friends
File "./friends", line 11
SyntaxError: invalid syntax

Please note, this was not happening a few hours ago when I was working on the very same file, using terminal on my imac. I haven't altered that line, and I'm absolutely stumped as to why it's playing up!

  • also won't work on my pc's cygwin, or another borrowed mac.
  • and yes, i've entered chmod +x friends into terminal.

It's probably the simplest, silliest thing, and I've probably completely overlooked something or accidentally bumped a key (it's 1am.. this is due on Monday...), but any help will be much appreciated!

share|improve this question
I'm almost sure there's a missing { or ] in your code somewhere (on one of the files). Anyway, there's another syntax error, not on this line –  jadkik94 May 4 '12 at 15:31
That usually points to unbalanced parenthesis and/or braces. But in this code example, it's fine. But you do have another problem. your first print statement will not work, 'phone' isn't accessible that way, not to mention the indenting. you'll want to access it differently, check out the newer .format() for strings in python. –  Mike Ramirez May 4 '12 at 15:33
Side note: if you're just learning Python, I'd recommend using an editor with syntax checking. My preferred blend is Sublime Text 2 + sublimelint –  lunixbochs May 4 '12 at 15:33
In my experience, this sort of SyntaxError indicating a valid line usually means you've got a syntax error on an adjacent line that's making Python interpret the line indicated as part of a multiline expression. –  Aesin May 4 '12 at 15:34
add comment

2 Answers

up vote 2 down vote accepted

The raw_input() line works fine here.

You have a syntax error on the "unsure" line. It could read like this:

print name + "'s phone number is: " + friends[name]['phone']

You could also use format strings:

print "%s's phone number is: %s" % (name, friends[name]['phone'])

Full if block:

if name in friends: 
    print 'no data'

Otherwise, your code worked for me after copying/pasting directly from your post.

share|improve this answer
Thank you so much for your help - like I said, half asleep, didn't see the missing closing }. –  user1374310 May 4 '12 at 15:39
also @lunixbochs your 'if' block worked! thank you, thank you :) –  user1374310 May 4 '12 at 15:41
add comment

You have syntax errors in the line that prints the message and forgot to indent it (that's not optional in python!).

Here are some fixed versions:


The other lines are perfectly fine, so the error you are getting is rather odd.

share|improve this answer
add comment

Your Answer


