Welcome to Dream.In.Code
Become an Expert!

Join 149,608 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,888 people online right now. Registration is fast and FREE... Join Now!




Python PhoneBook

 
Reply to this topicStart new topic

Python PhoneBook, converting user input to an object

mattman059
25 Aug, 2007 - 07:28 AM
Post #1

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 361


Dream Kudos: 175
My Contributions
I'm kinda new to python and i attended a tutorial the other day on basic/advanced methods in python. I went back to my appt. and decided to make a phone book.
This project consists of 2 files : 1) the phonebook (organized by county) and 2) the code for find().

CODE

def find():
    a = raw_input("What county do they live in?")
    b = raw_input("What is their First Name?")

    if a.has_key(b):
           print a[b]
    else:
           print "No Such Name."



But when i run the code i get this error:

CODE

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    find()
  File "G:\Python25\Find2.py", line 5, in find
    if a.has_key(b):
AttributeError: 'str' object has no attribute 'has_key'


I am wondering if there is a way to cast a string object as a dictionary so that i can look up the name and return the phone number. Thank you


User is offlineProfile CardPM
+Quote Post

kuratkull
RE: Python PhoneBook
27 Aug, 2007 - 04:21 AM
Post #2

New D.I.C Head
Group Icon

Joined: 1 May, 2007
Posts: 6


Dream Kudos: 250
My Contributions
CODE

def find(database):
    a = raw_input("What is their First Name?")

    if database.has_key(a) :
           print database[a]
    else:
           print "No Such Name."


It could be written more neatly, but I tried to modify your code as little as I could.

This post has been edited by kuratkull: 27 Aug, 2007 - 04:22 AM
User is offlineProfile CardPM
+Quote Post

mattman059
RE: Python PhoneBook
27 Aug, 2007 - 04:34 AM
Post #3

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 361


Dream Kudos: 175
My Contributions
is that the only way to do it? because I kind of wanted the user to be prompted for the name of the county
User is offlineProfile CardPM
+Quote Post

Nova Dragoon
RE: Python PhoneBook
28 Aug, 2007 - 05:30 AM
Post #4

The Innocent Shall Suffer, Big Time
Group Icon

Joined: 16 Aug, 2001
Posts: 6,145



Thanked: 8 times
Dream Kudos: 515
Expert In: Python, Linux

My Contributions
raw_input returns a string,

you are then querying the string like a dictionary. all it holds is what you assigned it with the raw_input method.

You'll need to do the lookup on the actual dictionary/list that contains the phone book.


and kuratkull, we don't just give people the answer without explaination. We help them achieve it for themselves.
User is offlineProfile CardPM
+Quote Post

Sharke
RE: Python PhoneBook
2 Dec, 2007 - 08:04 PM
Post #5

New D.I.C Head
*

Joined: 2 Dec, 2007
Posts: 6


My Contributions
I think what you need to do is to create a dictionary of dictionaries - that way you can refer to a dictionary with a variable containing it's name. For example:

CODE
mainDict = {"Brooklyn":Brooklyn, "Bronx":Bronx, "Manhattan":Manhattan}


Brooklyn, Bronx and Manhattan are dictionaries within the dictionary "mainDict". Their keys are strings, so you can reference them using a variable containing user input:

CODE
borough = raw_input("What borough? >> ")
name = raw_input("What name? >>")

d = mainDict[borough]
if d.has_key(name):
    print d[name]
else:
    print "No such name."


This post has been edited by Sharke: 2 Dec, 2007 - 08:06 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:05AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month