0

I'm creating a address book program, and need to have a dictionary that I can add too, edit, and delete, as well as pickle. What would be the best way to create it so it is accessible by all the functions? I currently have the dictionary in the addon function but wouldn't it reset if I were to call the dictionary to another function?

My code so far (not including the menuModule)

def addPerson():
personLastName = input("Enter the last name of "
                   "the person you want to add: ").lower()
personFirstName = input("Please enter the first name of "
                        "the person you want to add: ")

localPart = input("Please enter the local part of the email address")
while not localPart.isalnum():
    localPart = input("Please enter a valid input, a-z and numbers 0-9: ")

domain = input("Please enter the domain of the email addres: ")
while not domain.isalnum():
    domain = input("Please enter a valid input, a-z and numbers 0-9: ")

topLevelDomain = input("Please enter the top level domain, examples: com, net, org: ")
while not topLevelDomain.isalnum() or len(topLevelDomain) > 3:
     topLevelDomain = input("Please enter only letters, a-z and not more then 3 characters: ")

personEmail = localPart + "@" + domain + "." + topLevelDomain

personStreetAddress = input("Please enter house number and street of the person you want to add: ")
personCityState = input("Please enter the city, state abbreviation and zipcode of the person you want to add: ")

personPhone = input("Please enter the phone number of the person you want to add: ")

personPhoneStr = personPhone.strip("-")

while not personPhoneStr.isdigit() and not len(personPhoneStr) == 10:
    personPhone = input("Error. That is not a valid phone number. Try again: ")

    personPhoneStr = personPhone.strip("-")

return personLastName, personFirstName, personEmail, personStreetAddress, personCityState, personPhone




def appendDictionary():
personLastName, personFirstName, personEmail, personStreetAddress, personCityState, personPhone = addPerson()

listX = [personFirstName, personEmail, personStreetAddress, personCityState, personPhone]

addressBook = {personLastName: listX}

print(personFirstName,personLastName, "has been added to the address book!")

print(addressBook)

return addressBook
WonderphuL
  • 57
  • 8
  • see [423379](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – Munir Oct 29 '15 at 22:12

1 Answers1

0

Try using lists. One list for each of the variables because if you try to store them as a tuple and then add them into a master list you will not be able to or it will be hard to charge them and edit them. Here is an example of storing the data:

nameList.extend(john)
emailList.extend(john@gmail.com.uk)

john_index = len(nameList)

Give each person an index to help you file their information so if our list looked like [jane@live.com, sam@wenston.com, john@gmail.com.uk] johns data is going to be the last in the list because we just entered it in position 3 on the list and the length function returns 3 so you know where johns data is stored and if you were to add more data it would stack up accourdingly.

here is an example of getting it back out of the list and editing it:

print nameList[john_index]
print emailList[john_index]

emailList[john_index] = new_value

I hope you understand :)

J.Clarke
  • 392
  • 2
  • 15