[英]Python- dictionary initialization
我需要有三个名称和电子邮件地址才能开始该程序。 我尝试使用emailaddress = {"Drew": drew@myemail.com, "Lucie": lucie@snailmail.com, "Brodie": brodie@geemail.com}
但这会给我以下信息:
追溯(最近一次通话):
文件“ C:/ Users / Drew / Documents / CIT 144 / Email dictionary.py”,第17行
emailaddress = {“提请”:drew@myemail.com,“露西”:lucie@snailmail.com,
NameError:未定义名称“ drew”
该程序将与emailaddress = {}
,但如果具有键/值,则会出现该错误。 我是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()
您只是在声明不带引号的字符串,使python尝试解释它们而失败。
emailaddress = {"Drew": "drew@myemail.com", "Lucie": "lucie@snailmail.com", "Brodie": "brodie@geemail.com"}
将工作。 注意电子邮件周围的引号
emailaddress = {"Drew": drew@myemail.com, "Lucie": lucie@snailmail.com,
"Brodie": brodie@geemail.com}
drew@myemail.com不是字符串。 应该用引号引起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.