开发者

Find a way to add a string to a tuple

y="Peter Email: peter@rp.com Phone: 91291212"
z="Alan Email: alan@rp.com Phone: 98884444"
w="John Email: john@rp.com Phone: 93335555"
add_book=str(y) ,"" + str(z) ,"" + str(w)

**I am trying to add a contact into my address book but I am not sure how to add the string "details" into the add_book. I also found that I cannot use append because its a tuple.

details = raw_input("Enter name in the following format: name Email: Phone:")                      
        print "New contact added"
        print details 
if details in add_book:
    o=add_book+details
    print "contact found"
    print details
print add_book

address_book = {}
address_book['Alan'] = ['alan@rp.com, 912开发者_运维百科34567']#this is what I was supposed to do:

#but when I print it out, the output I get is:

{'Alan': ['alan@rp.com, 91234567']} #but I want to remove the '' and {}

I am still an amateur in programming with python so I really need all the help I can get, thanks:)!!


A simple fix would be to use a list instead of a tuple. You can do this by changing your initialization of add_book from:

add_book=str(y) ,"" + str(z) ,"" + str(w)

to:

add_book = [y,z,w]
#No need to call str() every time because your data are already strings

However, wouldn't it make more sense to organize your data as a list of dictionaries? For example:

contacts = ["Peter", "Alan", "John"]
addr_book = [len(contacts)]

for i in range(len(contacts)):
    contact = contacts[i]
    email= raw_input(contact+"'s email: ")
    phone= raw_input(contact+"'s phone: ")

    addr_book[i] = {'name':contact, 'email':email, 'phone':phone}

FURTHERMORE:
If I understood your question correctly, you have specific requirements as to how the output of your program should look. If you use the above data format, you can create whatever output you like. for example, this code

def printContact(contact):
    print contact['name']+': ['+contact[email]+','+contact[phone]+']'

will output something like:

Alan: [alan@email.com,555-555-5555]

Of course you can change it however you like.


firstly [] is a list. a tuple is (,);

so what you want is

address_book['Alan'] = ('alan@rp.com', '91234567')

But this seems quite odd. What i would do is create a class

class Contact(object):
    name = "Contact Name"
    email = "Contact Email"
    ph_number = "00000000"

    def __str__(self):
        return "%S: %s, %s" % (self.name, self.email, self.ph_number)

then

address_book = []
contact_alan = Contact()
contact_alan.name = "Alan"
contact_alan.email = "alan@rp.com"
contact_alan.ph_number = "91234567"

print contact

(not next to a machine with python so it might be slightly wrong. Will test it when i can get to one.)

EDIT:- as Paul pointed out in his comment:

class Contact(object):

    def __init__(self, name, email, ph_number):
        self.name = name
        self.email = email
        self.ph_number = ph_number

contact_alan = Contact(name="Alan", email = "alan@rp.com", ph_number="91234567")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜