#πŸ”’ Exercise help

76 messages Β· Page 1 of 1 (latest)

neat rose
meager meadowBOT
#

@neat rose

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.

neat rose
#

what is this exercising asking me to do?

#

Im just confused with

#

why is there name: alice?

hallow escarp
charred wraith
hallow escarp
#

It seems they're unfamiliar with dicts which is the issue

charred wraith
charred wraith
hallow escarp
charred wraith
#

Yh i guess this type of input :

dict[name]
#

No ?

hallow escarp
#

yup

charred wraith
#

Yh then you just do this :
||

print(student[name])
```||
hallow escarp
charred wraith
#

Or

print("Name:", student["name"])
hallow escarp
#

That's still not using a correct string as the key

charred wraith
#

Better

hallow escarp
#

yes πŸ™‚

neat rose
#

could you explain me the practical note point?

hallow escarp
neat rose
#

yes i undeerstand nesting

#

not .get()

#

I dont understand their point

hallow escarp
#

!e

student = {'name': 'Alice'}
print(student['name'])
meager meadowBOT
hallow escarp
#

we can access a key like so, but, if the key does not exist, it will error

#

!e

student = {'person': 'Alice'}
print(student['name'])
meager meadowBOT
neat rose
#

I understand that it doesnt crash while using .get() because it would just return None, but what purpose does that achieve

hallow escarp
#

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'

hallow escarp
#

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'))
meager meadowBOT
hallow escarp
#

this returns the value if the key exists, but if it doesn't, we return an alternate value

neat rose
#

but our whole goal is to get a value from the nested dict right?

#

why do we want an alternative value

hallow escarp
#

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

neat rose
hallow escarp
#

the type doesn't change in success/failure

neat rose
#

where didd u get info from

hallow escarp
# neat rose why info?

the name doesn't matter, I just picked a random key name that could potentially have a dict as its value

hallow escarp
#

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"

neat rose
#

why get an error in the first place?

hallow escarp
#

keeping the type consistent will help avoid future errors

neat rose
#

if the value is right there in the nested ddict, what can possibly go wrong?

hallow escarp
#

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', {})
neat rose
hallow escarp
#

!e

student = {'person': 'Alice'}
info = student.get('info')
print(info.get('grades'))

meager meadowBOT
hallow escarp
#

'NoneType' object has no attribute 'get'

neat rose
#

ok so its still ok to use the standard way?

hallow escarp
#

we use .get() when there's the possibility that the key might not exist

#

otherwise, use getitem syntax

#

dict[key]

meager meadowBOT
#
Python help channel closed for inactivity

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.