0

Hi all, I am reading through the Non-Programmer's Tutorial and I would like to make a program with Python in which is similar to the one in the example.

The difference with the program in this example is that you can only assign a number to a name, whereas in my program I would like to assign several variables to a number; for example I want the user to add events in, and give information like date, time, location and description.

def print_menu():
    print '1. Print Phone Numbers'
    print '2. Add a Phone Number'
    print '3. Remove a Phone Number'
    print '4. Lookup a Phone Number'
    print '5. Quit'
    print
 
numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 5:
    menu_choice = input("Type in a number (1-5): ")
    if menu_choice == 1:
        print "Telephone Numbers:"
        for x in numbers.keys():
            print "Name: ", x, "\tNumber:", numbers[x]
        print
    elif menu_choice == 2:
        print "Add Name and Number"
        name = raw_input("Name: ")
        phone = raw_input("Number: ")
        numbers[name] = phone
    elif menu_choice == 3:
        print "Remove Name and Number"
        name = raw_input("Name: ")
        if name in numbers:
            del numbers[name]
        else:
            print name, "was not found"
    elif menu_choice == 4:
        print "Lookup Number"
        name = raw_input("Name: ")
        if name in numbers:
            print "The number is", numbers[name]
        else:
            print name, "was not found"
    elif menu_choice != 5:
        print_menu()

So for example I don't know if the numbers dictionary will work here?

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by SoulMazer
1

Yes, you can make it work by using a tuple as the value. Example:

numbers = {}

name = "Mario"
number = "555-5555"
address = "322 Main Street"
email = "mlopez@aol.com"

numbers[name] = (number, address, email)

print numbers["Mario"][0]  # 555-5555
print numbers["Mario"][1]  # 322 Main Street
print numbers["Mario"][2]  # mlopez@aol.com

Edited by SoulMazer: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.