简体   繁体   English

Python字典初始化

[英]Python- dictionary initialization

I need to have three names and email addresses to begin the program with. 我需要有三个名称和电子邮件地址才能开始该程序。 I have tried to use emailaddress = {"Drew": drew@myemail.com, "Lucie": lucie@snailmail.com, "Brodie": brodie@geemail.com} but it will give me this: 我尝试使用emailaddress = {"Drew": drew@myemail.com, "Lucie": lucie@snailmail.com, "Brodie": brodie@geemail.com}但这会给我以下信息:

Traceback (most recent call last): 追溯(最近一次通话):

File "C:/Users/Drew/Documents/CIT 144/Email dictionary.py", line 17, 文件“ C:/ Users / Drew / Documents / CIT 144 / Email dictionary.py”,第17行

emailaddress = {"Drew": drew@myemail.com, "Lucie": lucie@snailmail.com, emailaddress = {“提请”:drew@myemail.com,“露西”:lucie@snailmail.com,

NameError: name 'drew' is not defined NameError:未定义名称“ drew”

The program will work with emailaddress = {} , but gives that error if it has keys/values. 该程序将与emailaddress = {} ,但如果具有键/值,则会出现该错误。 I am new to Python and programming, so any help or explanation is appreciated greatly!!! 我是Python和程序设计的新手,因此非常感谢您的帮助或解释!!!

## This program keeps names and email addresses
# in a dictionary called emailaddress as key-value pairs.
# The program should initialize the dictionary with three
# people/email addresses. Then program should display the
# menu and loop until 5 is selected
##

def displayMenu():
    print()
    print("1) Look up email address")
    print("2) Add a name and email address")
    print("3) Change email address")
    print("4) Delete name and email address")
    print("5) End program")
    print()

emailaddress = {}
choice = 0
displayMenu()
while choice != 5:
    choice = int(input("Enter your selection (1-5): "))

    if choice == 1:
        print("Look up email address:")
        name = input("Name: ")
        if name in emailaddress:
            print("The email address is", emailaddress[name])
        else:
            print(name, "was not found")
    elif choice == 2:
        print("Add a name and email address")
        name = input("Name: ")
        email = input("Email: ")
        emailaddress[name] = email
    elif choice == 3:
        print("Change email address")
        name = input("Name: ")
        if name in emailaddress:
            email = input("Enter the new address: ")
            emailaddress[name] = email
        else:
            print(name, "was not found")
    elif choice == 4:
        print("Delete name and email address")
        name = input("Name: ")
        if name in emailaddress:
            del emailaddress[name]
        else:
            print(name, "was not found")          
    elif choice != 5:
        print("Enter a valid selection")
        displayMenu()

You are simply declaring your strings without quotes, making python try to interpret them and fail. 您只是在声明不带引号的字符串,使python尝试解释它们而失败。

emailaddress = {"Drew": "drew@myemail.com", "Lucie": "lucie@snailmail.com", "Brodie": "brodie@geemail.com"}

will work. 将工作。 Notice the quotes around the email 注意电子邮件周围的引号

emailaddress = {"Drew": drew@myemail.com, "Lucie": lucie@snailmail.com, 
"Brodie": brodie@geemail.com}

drew@myemail.com is not a string. drew@myemail.com不是字符串。 It should be in quotations. 应该用引号引起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM