在python中将字典传递给新函数?

在python中将字典传递给新函数?,python,dictionary,parameter-passing,pickle,Python,Dictionary,Parameter Passing,Pickle,如果我创建了一个需要在多个函数中访问的字典,那么传递它的最佳方式是什么 我目前正在做的是将字典重置为空。如果我在addDictionary()中打印,我会得到想要的结果。但是,当我使用lookUpEntry()中的键查找元素时,我找不到它。当我打印时,我得到一本空字典。我最终也要泡菜解馋,所以如果有人对此有任何反馈,那也会有帮助 import pickle def dictionary(): addressBook = {} return addressBook def addPerson

如果我创建了一个需要在多个函数中访问的字典,那么传递它的最佳方式是什么

我目前正在做的是将字典重置为空。如果我在addDictionary()中打印,我会得到想要的结果。但是,当我使用lookUpEntry()中的键查找元素时,我找不到它。当我打印时,我得到一本空字典。我最终也要泡菜解馋,所以如果有人对此有任何反馈,那也会有帮助

import pickle

def dictionary():
addressBook = {}

return addressBook

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 addDictionary():
addressBook = dictionary()

personLastName, personFirstName, personEmail, personStreetAddress, personCityState, personPhone = addPerson()

addressBook[personLastName] = personFirstName, personEmail, personStreetAddress, personCityState, personPhone

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

print(addressBook)

return addressBook


def lookUpEntry():
addressBook = dictionary()

keyName = input("Enter the last name of the person you are trying to find.")

while not keyName in addressBook:
    keyName = input("That name is not in the address book. Please try again.").lower()

x = input("Enter '1' if you want to look up a email. Enter '2' if you want to look "
          "up a persons address. Enter '3' to look up a persons phone number: ")
if x == "1":
    print("The email of", addressBook[keyName[0]], keyName, "is:", addressBook[keyName[1]])
elif x == "2":
    print("The address of", addressBook[keyName[0]], keyName, "is:", addressBook[keyName[2]], addressBook[keyName[3]])
elif x == "3":
    print("The phone number of", addressBook[keyName[0]], keyName, "is:", addressBook[keyName[4]])
else:
    print("Sorry that item is not stored in this address book.")

def main():
addDictionary()
lookUpEntry()

main()

当前您将
字典定义为

def dictionary():
    addressBook = {}
    return addressBook
在这里,每次调用字典时都会创建一个新字典。尝试将此替换为

# a global dictionary
_addressBook = {}
def dictionary():
    return _addressBook

请按照当前代码的位置正确缩进您的代码,如果您不将
dict
传递到
addDictionary
lookupEntry
,则几乎需要一个globalOkay。传递dict的最佳方法是什么?很抱歉,我使用了mono,错过了向函数传递值的课程。不是我的强项。