user7577503
user7577503

Reputation:

Python- dictionary initialization

I need to have three names and email addresses to begin the program with. I have tried to use emailaddress = {"Drew": [email protected], "Lucie": [email protected], "Brodie": [email protected]} 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": [email protected], "Lucie": [email protected],

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()

Upvotes: 0

Views: 144

Answers (2)

Abhishek Tumuluru
Abhishek Tumuluru

Reputation: 155

emailaddress = {"Drew": [email protected], "Lucie": [email protected], 
"Brodie": [email protected]}

[email protected] is not a string. It should be in quotations.

Upvotes: 0

WNG
WNG

Reputation: 3815

You are simply declaring your strings without quotes, making python try to interpret them and fail.

emailaddress = {"Drew": "[email protected]", "Lucie": "[email protected]", "Brodie": "[email protected]"}

will work. Notice the quotes around the email

Upvotes: 1

Related Questions