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:
Traceback (most recent call last):
File "C:/Users/Drew/Documents/CIT 144/Email dictionary.py", line 17,
emailaddress = {"Drew": drew@myemail.com, "Lucie": lucie@snailmail.com,
NameError: name 'drew' is not defined
The program will work with emailaddress = {}
, but gives that error if it has keys/values. I am new to Python and programming, so any help or explanation is appreciated greatly!!!
## 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.
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. It should be in quotations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.