#π Exercise help
76 messages Β· Page 1 of 1 (latest)
@neat rose
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.
what is this exercising asking me to do?
Im just confused with
why is there name: alice?
Hint semi-spoiler:
||Because you need to print it with print()||
That's not what it's asking them to do
It seems they're unfamiliar with dicts which is the issue
Itβs asking them to change the values in dict and print smthg no ?
Yh i see that
It just says access, it doesn't say print
yup
Yh then you just do this :
||
print(student[name])
```||
almost, the key here would be a string
Or
print("Name:", student["name"])
That's still not using a correct string as the key
Oh bruh i didnnt catch it lol
Better
yes π
actually I found the solution and understand it but i have a new problem
could you explain me the practical note point?
Do you understand what nesting means? What about .get()?
!e
student = {'name': 'Alice'}
print(student['name'])
:white_check_mark: Your 3.14 eval job has completed with return code 0.
Alice
we can access a key like so, but, if the key does not exist, it will error
!e
student = {'person': 'Alice'}
print(student['name'])
:x: Your 3.14 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m2[0m, in [35m<module>[0m
003 | print([31mstudent[0m[1;31m['name'][0m)
004 | [31m~~~~~~~[0m[1;31m^^^^^^^^[0m
005 | [1;35mKeyError[0m: [35m'name'[0m
I understand that it doesnt crash while using .get() because it would just return None, but what purpose does that achieve
it's easier to pair it with a condition (even though you could do the same without it)
if student.get('name')
you could also do if 'name' in student'
wdym pair it with a condition?
but another advantage of get() is we can choose an alternative value in case of failure
!e
student = {'person': 'Alice'}
print(student.get('name', 'UNKNOWN'))
:white_check_mark: Your 3.14 eval job has completed with return code 0.
UNKNOWN
this returns the value if the key exists, but if it doesn't, we return an alternate value
but our whole goal is to get a value from the nested dict right?
why do we want an alternative value
student.get('info', {})
because we could do something like this
if we're expected a dict value but the key doesn't exist, at least we still get a dict response
why info?
the type doesn't change in success/failure
where didd u get info from
the name doesn't matter, I just picked a random key name that could potentially have a dict as its value
I see
so if I expect the info key to have a dict as its value
I'm basically saying "Give me the dict if it exists, or give me an empty dict if it doesn't"
why get an error in the first place?
keeping the type consistent will help avoid future errors
if the value is right there in the nested ddict, what can possibly go wrong?
nothing will go wrong if it is there
but look at this example
student = {'person': 'Alice'}
info = student.get('info')
grades = info.get('grades')
'info' doesn't exist
so it will return None
None.get('grades') will error
student = {'person': 'Alice'}
info = student.get('info', {})
grades = info.get('grades', {})
why are u doing this code?
because get() will return None if the key doesn't exist
!e
student = {'person': 'Alice'}
info = student.get('info')
print(info.get('grades'))
:x: Your 3.14 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m3[0m, in [35m<module>[0m
003 | print([1;31minfo.get[0m('grades'))
004 | [1;31m^^^^^^^^[0m
005 | [1;35mAttributeError[0m: [35m'NoneType' object has no attribute 'get'[0m
'NoneType' object has no attribute 'get'
ok so its still ok to use the standard way?
we use .get() when there's the possibility that the key might not exist
otherwise, use getitem syntax
dict[key]
This help channel has been closed. 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.