Python noob needs help.

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Python noob needs help.

Post by cthulhu666 on Wed Sep 14, 2011 9:09 am
([msg=61537]see Python noob needs help.[/msg])

So I just started learning python and while going through a tutorial I read that you can have lists in dictionaries. I went back to an earlier example with dictionaries to try and add a list as a value to some key. After a couple hours ( yes, hours -_-) I managed to successfully accomplish gathering the data as intended.

First I'll show the original (to give you perspective on what i've done).
Code: Select all
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 = int(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 = input("Name: ")
        phone = input("Number: ")
        numbers[name] = phone
    elif menu_choice == 3:
        print("Remove Name and Number")
        name = input("Name: ")
        if name in numbers:
            del numbers[name]
        else:
            print(name, "was not found")
    elif menu_choice == 4:
        print("Lookup Number")
        name = input("Name: ")
        if name in numbers:
            print("The number is", numbers[name])
        else:
            print(name, "was not found")
    elif menu_choice != 5:
        print_menu()


I basically just wanted to add another value for age using dictionaries with lists (dict.key(list)) so I got this.
Code: Select all
dictio = {}

def get_info():
    print("Add Name and Number")
    listy = []
    name = input("Name: ")
    phone = input("Number: ")
    age = input("Age: ")
    listy.append(phone)
    listy.append(age)
    dictio[name] = listy

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

menu_choice = 0
print_menu()

while menu_choice != 5:
    menu_choice = int(input("Type in a number (1-5): "))
    if menu_choice == 1:
        print("Telephone Numbers:")
        for x in dictio.keys():
            print("Name: ", x, "\tNumber:", dictio[x])
        print()
    elif menu_choice == 2:
        get_info()
    elif menu_choice == 3:
        print("Remove Name and Number")
        name = input("Name: ")
        if name in dictio:
            del dictio[name]
        else:
            print(name, "was not found")
    elif menu_choice == 4:
        print("Lookup Number")
        name = input("Name: ")
        if name in dictio:
            print("The number is", numbers[name])
        else:
            print(name, "was not found")
    elif menu_choice != 5:
        print_menu()


Basically I'm still now sure how to output the first (and I guess fourth) menu items.

Any help would be appreciated. :D
cthulhu666
New User
New User
 
Posts: 22
Joined: Tue Apr 15, 2008 12:27 pm
Blog: View Blog (0)


Re: Python noob needs help.

Post by pix6ie on Thu Sep 15, 2011 4:15 pm
([msg=61578]see Re: Python noob needs help.[/msg])

If you are tryin to ouytput the values in the dictionary you have to enter the name you assigned it,

say:

dict = {}
name = 'joe'
number = '123'
name2 = 'steve'
number2 = '456'
list = [name, number]
list2 = [name2, number2]
Now you have two lists and two different values to put into the dict.
dict['joe'] = list
dict['Steve'] = list2
now if you want to view joes data you type
dict['joe']
or to view steves
dict['steve']

These will print out the lists of data containing the names and numbers of joe and steve.
Hope this was of some help
pix6ie
New User
New User
 
Posts: 1
Joined: Thu Nov 18, 2010 7:04 pm
Blog: View Blog (0)


Re: Python noob needs help.

Post by cthulhu666 on Fri Sep 16, 2011 10:03 am
([msg=61589]see Re: Python noob needs help.[/msg])

Hmm no offense pix6ie but that really doesn't help much -_-. Let me try to be more specific...

Let's say i have a dictionary called dict. This dictionary's key's contain lists such as...
dict[x] = (key: (list1, list2))

I need some way to print the first item of list separately from the second. To give you some context...
Code: Select all
for x in dicti.keys():
            print("Name: ", x, "\tNumber:", dictio[x] #NEED LIST 1, "Age: ", dictio[x] NEED LIST 2)


FIXED troloolol. All I had to do was....
Code: Select all
for x in dictio.keys():
            print("Name: ", x, "\tNumber:", dictio[x][0], "\tAge: ", dictio[x][1])


I feel kind of silly...
cthulhu666
New User
New User
 
Posts: 22
Joined: Tue Apr 15, 2008 12:27 pm
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests