Advertisement
robertvari

teszt

Oct 27th, 2018
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. # -*- coding:Utf-8 -*-
  2. import json, os, time
  3.  
  4. contactData = {}
  5.  
  6. def main():
  7.     # get existing contact data
  8.     loadData()
  9.  
  10.     print "Hello! This is your contact list."
  11.  
  12.     print """
  13.    1. print contact list
  14.    2. Add contact
  15.    3. Delete contact
  16.    4. Exit
  17.    """
  18.  
  19.     functionList = {"1":printContact, "2":addContact, "3":deleteContact, "4":exit}
  20.  
  21.     userInput = raw_input()
  22.     if userInput in functionList:
  23.         functionList[userInput]()
  24.  
  25. def printContact():
  26.  
  27.     for name, data in contactData.items():
  28.         print name
  29.         print "\t", "phone:", data["phone"]
  30.         print "\t", "address:", data["address"]
  31.  
  32.     raw_input()
  33.  
  34.     print "Returning to main menu..."
  35.     time.sleep(3)
  36.     main()
  37.  
  38. def addContact():
  39.     global contactData
  40.  
  41.     name = raw_input("Name?")
  42.     phone = raw_input("Phone?")
  43.     address = raw_input("Address?")
  44.  
  45.     contactData[name] = {"phone":phone, "address":address, "notes":None}
  46.  
  47.     userInput = raw_input("Do you want to add more contacts? (y/n)")
  48.     if userInput.lower() == "y":
  49.         addContact()
  50.  
  51.     saveData()
  52.  
  53.     print "Returning to main menu..."
  54.     time.sleep(3)
  55.     main()
  56.  
  57. def deleteContact():
  58.     global contactData
  59.  
  60.     print "Your contact list:"
  61.     for k, v in contactData.items():
  62.         print k, v
  63.  
  64.     name = raw_input("Name to delete?")
  65.  
  66.     if name in contactData:
  67.         contactData.pop(name)
  68.         print "{} was deleted from contact.".format(name)
  69.  
  70.     saveData()
  71.  
  72.     userInput = raw_input("Do you want to delet contacts? (y/n)")
  73.     if userInput == "y":
  74.         deleteContact()
  75.  
  76.     print "Returning to main menu..."
  77.     time.sleep(3)
  78.     main()
  79.  
  80.  
  81. # *************************** utility functions *****************************
  82. def loadData():
  83.     global contactData
  84.  
  85.     if os.path.exists("contactData.json"):
  86.         with open("contactData.json") as dataFile:
  87.             contactData = json.load(dataFile)
  88.  
  89. def saveData():
  90.     with open("contactData.json", "w") as dataFile:
  91.         json.dump(contactData, dataFile)
  92.  
  93. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement