#πŸ”’ Need some help with list and dictionaries

34 messages Β· Page 1 of 1 (latest)

glossy vapor
#

Hey so I am currently trying to make a dictionary of names that pulls up a list of info already associated with that name. so far I have (dont worry this is fake info) ```def student_dict():
student_name = {'syd': student[0]}
student = ['id: ,gpa: , phone:, email']
student['syd'] = ['id: 123456789 ,gpa: 3.27 , phone: 520-555-5555, email: [email protected]']

    print(student_name['syd'])```

but it isnt working and im a little uncleat on how list work in general so im worried i just might be going about it all wrong but youtube and google arent making it any clearer. Is this approach completely wrong what am I missing?

north rampartBOT
#

@glossy vapor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

timid goblet
#

I can't figure out what you're trying to do there

#

maybe just ```py
student_info_by_first_name = {
"syd": {
"id": "123456789",
"gpa": 3.27,
"phone": "520 - 555 - 5555",
"email": "[email protected]",
},
}

glossy vapor
main nebula
#

What if there are two students with the same name, should there be two lists?

glossy vapor
#

so it has to unfortunately have both

glossy vapor
#

the exact expectation is: "Write an instance method called student_dict (3 pts) that takes no arguments and returns a dictionary that maps the student's name to a list containing his/her student information. The list will have the order id_number, gpa, phone, email."

timid goblet
#

why would you want a list of info, and not a dict?

#

ah because the assignment wants it

glossy vapor
#

yes sir

timid goblet
#

yeah of course

main nebula
#

I don't see why the proposed solution doesn't work except with a list.

timid goblet
#
student_info_by_first_name = {
    "syd": [
        123456789,
        3.27,
        "520 - 555 - 5555",
        "[email protected]",
    ],
}


def stupid_assignment_function(name):
    return student_info_by_first_name[name]
main nebula
#

Well, the question says instance method

#

I'm assuming you have a class.

timid goblet
#

"left as an exercise for the reader" πŸ™‚

glossy vapor
timid goblet
#

sorry I have no idea what that means

glossy vapor
#

so like instead of a list of "students" where syd is student[0] its a individual list per student yeah?

main nebula
#

If you have a method instance you are operating within a class

timid goblet
#

that's the idea, yeah

glossy vapor
main nebula
# glossy vapor yeah i have all the class stuff before it but they dont need to interact and tha...

I didn't read your assignment, so this is not 100% correct, but you don't act as though you're inside a class right now.
You should have something SIMILAR to this:

class Student:
    def __init__(self, name, id_number, gpa, phone, email):
        self.name = name
        self.id_number = id_number
        self.gpa = gpa
        self.phone = phone
        self.email = email
        

    def student_dict(self):
        return {self.name:[self.id_number, self.gpa, self.phone, self.email]}
glossy vapor
#

yeah what i have is ```class Student:

def __init__(self, name, id_number, gpa, phone, email):
    self.name = name
    self.id_number = id_number
    self.gpa = gpa
    self.phone = phone
    self.email = email

def __repr__(self):
    result = (self.name + "\n" + "\n")
    result += (self.id_number + "\n" + "\n")
    result += (self.gpa + "\n" + "\n")
    result += (self.phone + "\n" + "\n")
    result += (self.email + "\n" + "\n")
    return result

def student_dict():
student_name = {'syd': student}

    student = ['id: 123456789  ,gpa: 3.27 , phone: 520-555-5555, email: 

[email protected]']

    print(student_name)```
main nebula
#

Then what I wrote is correct

#

You are asked to create an instance method called student_dict
it takes no arguments
it returns a dictionary

def student_dict(): #  Instance method with no arguments
  # Empty until we continue with the question
  return some_dictionary # Returns some dictionary

This dictionary "maps the student's name to a list containing his/her student information",and "The list will have the order id_number, gpa, phone, email."`
Lets define this list.

student_information_list = [self.id_number, self.gpa, self.phone, self.email]  # Now we have a list as expected

and Lastly, we are asked to map the name to this list.

some_dictionary = {self.name : student_information_list}  # Maps the name to the list
#

Putting it all together, we get

def student_dict(self):  # Instance method with no arguments
  student_information_list = [self.id_number, self.gpa, self.phone, self.email]  # Now we have a list as expected
  some_dictionary = {self.name : student_information_list}  # Maps the name to the list
  return some_dictionary  # Returns some dictionary

or, shortened

    def student_dict(self):
        return {self.name:[self.id_number, self.gpa, self.phone, self.email]}
glossy vapor
#

Got it thanks

#

!close

north rampartBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.