assignment help !!!

This is the place for queries that don't fit in any of the other categories.

assignment help !!!

Postby boulis229 » Sun Dec 08, 2013 6:11 pm

i have a dictionary with names and an age for each name and i want to write a function that takes in an age and returns the names of all the people who are that age.
Last edited by stranac on Sun Dec 08, 2013 6:15 pm, edited 1 time in total.
Reason: First post lock.
boulis229
 
Posts: 4
Joined: Sun Dec 08, 2013 6:06 pm

Re: assignment help !!!

Postby stranac » Sun Dec 08, 2013 6:16 pm

Good for you.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: assignment help !!!

Postby boulis229 » Sun Dec 08, 2013 6:21 pm

good for me?;p i need help here plz
boulis229
 
Posts: 4
Joined: Sun Dec 08, 2013 6:06 pm

Re: assignment help !!!

Postby stranac » Sun Dec 08, 2013 6:26 pm

What kind of help?
How much python do you know? What have you tried? What problems are you having?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: assignment help !!!

Postby boulis229 » Sun Dec 08, 2013 6:36 pm

i have this code:
Code: Select all
name = ['Alice', 'Bob', 'Cathy', 'Dan', 'Ed', 'Frank', 'Gary', 'Helen', 'Irene', 'Jack', 'Kelly', 'Larry']
age = [20, 21, 18, 18, 19, 20, 20, 19, 19, 19, 22, 19]
dic={}
def combine_lists(name,age):
    for i in range(len(name)):
        dic[name[i]]= age[i]
combine_lists(name,age)
print dic

def people(age):
    people=lambda age: [name for name in dic if dic[name]==age]

people(20)



but it doesn't give me any results
Last edited by stranac on Sun Dec 08, 2013 6:38 pm, edited 1 time in total.
Reason: Code tags
boulis229
 
Posts: 4
Joined: Sun Dec 08, 2013 6:06 pm

Re: assignment help !!!

Postby metulburr » Sun Dec 08, 2013 6:49 pm

Code: Select all
def people(age):
    people=lambda age: [name for name in dic if dic[name]==age]

for one you are not returning/dispalying anything, so obviously nothing will happen. Secondly I don't know why you have the lambda in there:
Code: Select all
def people(age):
    people= [name for name in dic if dic[name]==age]
    print(people)

also i might add the naming cenventions. It is not wise to name a local variable the same name as its function.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: assignment help !!!

Postby stranac » Sun Dec 08, 2013 6:49 pm

Read this: New Users, Read This

Your people() function doesn't make much sense.
Inside it, you use the lambda keyword to create a function which returns what you want the outer function to return.
Either of these two would work(I would recommend using the one with def):
Code: Select all
def people(age):
    return [name for name in dic if dic[name] == age]

# you don't ever wanna be using this one
people = lambda age: [name for name in dic if dic[name] == age]


Also, rather than mutating the global dic inside combine_lists(), you should create the dict inside it and return it.
Using dict() with zip() would make that function trivial.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: assignment help !!!

Postby boulis229 » Sun Dec 08, 2013 7:00 pm

thank you so much
boulis229
 
Posts: 4
Joined: Sun Dec 08, 2013 6:06 pm


Return to General Coding Help

Who is online

Users browsing this forum: W3C [Linkcheck] and 2 guests