Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. class phonebook():
  2.     def __init__(self):
  3.         self.phonedic = {}              #creates an empty dictionary
  4.         self.alias = {}
  5.        
  6.     def add(self, name, number):        #this function adds a contact
  7.         d = self.phonedic
  8.         for key, value in d.items():
  9.             if  number in value:
  10.                 print "That number already exist in the phonebook"
  11.                 return
  12.         self.phonedic[name] = number
  13.         self.alias[name] = name
  14.         print name , "has been added."
  15.     def lookup(self, name):         #this function can look up a contact
  16.         print "self.phonedic.has_key = " , self.phonedic[name]
  17.         print "self.alias.has_key= ", self.alias[name]
  18.    
  19.         if self.phonedic.has_key(name):
  20.             print name + ":s" , "number:" , self.phonedic[name]
  21.         elif self.alias.has_key(name):
  22.             print name + ":s" , "number:" , self.alias[name]
  23.         else:
  24.             print name , "does not exist."
  25.            
  26.     def alias1(self, name, nickname): #creates an alias for the contact, so it can be found under both names
  27.         print "namn" , name , "nickname:", nickname
  28.         if self.phonedic.has_key(name):
  29.             self.alias[nickname] = name
  30.             print "alias tillagd"
  31.             print self.alias
  32.         else:
  33.             print "Can't create an alias for a non-existent contact."
  34.            
  35.     def change(self, name, number): #changes the number of a contact   
  36.         if self.phonedic.has_key(name):
  37.             self.phonedic[name] = number
  38.             print "The new number is saved."
  39.         else:
  40.             print "Couldnt find the contact", name
  41.        
  42.     def save(self, filename):       #saves the phonebook to a file
  43.         f = open("C://Python27//Labbar//" + filename, "w")
  44.         for key, value in self.phonedic.items():
  45.             k = value + ";" + key + ";" + "\n"
  46.             f.write(str(k))
  47.         f.close()
  48.         print "File saved with the name: " , filename
  49.     def load(self, filename):      
  50.         f = open("C://Python27//Labbar//" + filename, "r")
  51.         for line in f:
  52.             x = line.split(";")
  53.             x[-1] = x[-1].strip("\n")
  54.             number = x[0]
  55.             name = x[1]
  56.             self.phonedic[name] = number
  57.         f.close()
  58. def main():
  59.     pb = phonebook()
  60.     while True:
  61.         prompt = raw_input("Phonebook>>")
  62.         x = prompt.split()
  63.         try:
  64.             function = x[0]
  65.            
  66.             if function == "add":
  67.                 pb.add(x[1],x[2])
  68.             elif function == "lookup":
  69.                 pb.lookup(x[1])
  70.             elif function == "save":
  71.                 pb.save(x[1])
  72.             elif function == "alias1":
  73.                 pb.alias1(x[1],x[2])
  74.             elif function == "change":
  75.                 pb.change(x[1],x[2])
  76.             elif function == "quit":
  77.                 break
  78.             elif function == "load":
  79.                 pb.load(x[1])
  80.             else:
  81.                 print "Non-existent function, try again."
  82.         except: IndexError
  83.        
  84. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement