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’:
#!/usr/bin/python
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
else:
print 'no data'
and this is the error it’s giving me:
$ ./friends
File "./friends", line 11
name = raw_input ('Who are you looking for? ')
^
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!
The
raw_input()line works fine here.You have a syntax error on the “unsure” line. It could read like this:
You could also use format strings:
Full
ifblock:Otherwise, your code worked for me after copying/pasting directly from your post.