Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding:Utf-8 -*-
- import json, os, time
- contactData = {}
- def main():
- # get existing contact data
- loadData()
- print "Hello! This is your contact list."
- print """
- 1. print contact list
- 2. Add contact
- 3. Delete contact
- 4. Exit
- """
- functionList = {"1":printContact, "2":addContact, "3":deleteContact, "4":exit}
- userInput = raw_input()
- if userInput in functionList:
- functionList[userInput]()
- def printContact():
- for name, data in contactData.items():
- print name
- print "\t", "phone:", data["phone"]
- print "\t", "address:", data["address"]
- raw_input()
- print "Returning to main menu..."
- time.sleep(3)
- main()
- def addContact():
- global contactData
- name = raw_input("Name?")
- phone = raw_input("Phone?")
- address = raw_input("Address?")
- contactData[name] = {"phone":phone, "address":address, "notes":None}
- userInput = raw_input("Do you want to add more contacts? (y/n)")
- if userInput.lower() == "y":
- addContact()
- saveData()
- print "Returning to main menu..."
- time.sleep(3)
- main()
- def deleteContact():
- global contactData
- print "Your contact list:"
- for k, v in contactData.items():
- print k, v
- name = raw_input("Name to delete?")
- if name in contactData:
- contactData.pop(name)
- print "{} was deleted from contact.".format(name)
- saveData()
- userInput = raw_input("Do you want to delet contacts? (y/n)")
- if userInput == "y":
- deleteContact()
- print "Returning to main menu..."
- time.sleep(3)
- main()
- # *************************** utility functions *****************************
- def loadData():
- global contactData
- if os.path.exists("contactData.json"):
- with open("contactData.json") as dataFile:
- contactData = json.load(dataFile)
- def saveData():
- with open("contactData.json", "w") as dataFile:
- json.dump(contactData, dataFile)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement