Teach Yourself Python #66 "Getting all the keys"

python
non-track-course

#1

Hey Everyone.

Im almost finished with this Python Tutorial
https://www.codecademy.com/courses/python-beginner-en-fymF4/0/1
(first exercise)

Im currently on #66 "Getting all the keys"
https://www.codecademy.com/en/courses/python-beginner-en-fymF4/7/5

I've created the Topic.... I'll post my question within..

Thanks DB


#2

So, I'm having a hard time getting my code right.

The Instructions ask:

"For each pet in pets, print the pet's name and species"

This exercise is using the mydict.keys() & mydict.values() methods....
Also seems to ask for a for loop...

My Code... a few variations of codes...

pets = {'Boxo': 'dog',
        'Coe': 'betta',
        'Niko': 'cat'}
        
print pets.keys()

print pets.values()

another one might be...

pets = {'Boxo': 'dog',
        'Coe': 'betta',
        'Niko': 'cat'}
        
for pet in pets.keys():
    print pet
for pet in pets.values():
    print pet

and another my still be....

pets = {'Boxo': 'dog',
        'Coe': 'betta',
        'Niko': 'cat'}
        
x = for pet in pets.keys():
    print pet
y = for pet in pets.values():
    print pet

print x
print y

the machine didnt like that one....

What am I missing here?
Anyone else fooling around with this tutorial?

Any help or tips are much appreciated.

DB


#3

Just tried this one as well with no success...

pets = {'Boxo': 'dog',
        'Coe': 'betta',
        'Niko': 'cat'}
        
for name in pets.keys():
    print name
for species in pets.values():
    print species

Its always a typo for me...
if its not the typo, I could be very easily missing an important point.

rarely do I come across the bona fide "bug"

Any help or tips are much appreciated.
DB


#4

Most of my codes are printing out the correct info.

Most of my errors are the simple "Opps, try again."

I've done the refresh the screen stuff....

etc, etc..

Thanks.
DB


#5

Hi David,

Maybe you are overthinking it. The exercise has no mention of the .values method. In fact, though the exercise mentions .keys, we don't even need to use it as well. This is because, if you do, let's say, for x in y, x will take the keys of y dictionary, so no need for for x in y.keys().

The simplest answer thus is:

# non-working indicative code that you have to fill
for each pet in pets:
    print the pet name
    print the pet species

#6

Awesome :slight_smile:
Let me get to work on this.
I'll get back in touch to let everyone know that I made it thru the step.

Thanks @gaurangtandon

Also, that link you sent me, that's pretty cool.
I look forward to tinkering around with that.


#7

Dang.

I've given those suggestions a few tries.
I've also tinkered with the repl.it website, (which is super cool !!)

but, Im coming up short.

I'll keep trying different variation

any other suggestions ? :slight_smile:

Thanks DB


#8

Here is the written section:

Getting all the keys
One useful method that comes with dictionaries is the ability to get all the keys for that dictionary. It looks like this:

> mydict.keys()

for state in states.keys():
    print state

This would print out each state that has been used as a key in states.

Here is the Instructions (the problem, #66 "Getting all the keys")

For each pet in pets, print the pet's name and species.

My different attempts for working the exercise are listed above.
I have successfully printed out the name and species...
I can't get the green light.

:anguished: :weary: :tired_face: :scream: :-1: :head_bandage:


#9

Oh, ok. Let me further expand my indicative code (remember that your code has to look similar to this):

"""
The pets dictionary looks like this:
pets = {
    pet_name: pets_species,
    pet_name2: pet_species2,
    etc.
}

remember that each key is a pet name and its corresponding value is a pet species.
"""

# non-working indicative code that you have to fill
for each pet in pets:     # make this a for-in loop
    print the pet name    # you now know what pet name is
    print the pet species # how do access you pet species (value of pet key) given the pet key?

I have to agree that the Submission Correctness Test of this exercise is not well written. It requires certain specific code fragments to be must present, and in fact it does not care about the output of your code!


#10

:hourglass: :hourglass_flowing_sand: :clock: :atom: :wheel_of_dharma:
Im meditating on this question.

Thank you for taking the time to troubleshoot my questions.

This is what I coded up this morning with no success :confused:

pets = {
    'Boxo': 'dog',
    'Coe': 'betta',
    'Niko': 'cat'
}

for name in pets:
    print name
    
print "\n"

for species in pets.values():
    print species
    
print "\n"

print pets

And the console posted this......

Boxo
Coe
Niko


dog
betta
cat


{'Boxo': 'dog', 'Coe': 'betta', 'Niko': 'cat'}
None

Still got my error.

No sweat. I'll keep pluggin' away.
I love me some Codecademy. !!

:fireworks:


#11

Actually, this course isn't written by CC staff but another external author. Another thing I see with your code, I have already told you that .values isn't taught in exercise 66 and you aren't expected to use it. Please follow the structure I've given you:

"""
The pets dictionary looks like this:
pets = {
    pet_name: pets_species,
    pet_name2: pet_species2,
    etc.
}

remember that each key is a pet name and its corresponding value is a pet species.
"""

# non-working indicative code that you have to fill
for each pet in pets:     # make this a for-in loop
    print the pet name    # you now know what pet name is
    print the pet species # how do access you pet species (value of pet key) given the pet key?

If you can't still get the answer, here's it:

Click to reveal code. Only do so when you've followed my template.

for pet in pets:
    print pets, pets[pet]


#12

Definitely. I will do as you suggest.

Also, I'll try to hold off from opening up the "reveal code"

I understood what you meant when talking about .values() :slight_smile:

I'll get my code right without using the .values()
looks like #67 will be dealing with the value side of things.

I came across this tutorial from a Java Script tutorial.....
Both being "Sam Teaches .... in 24hours."
I found the link in the CC Forums.
Its helping me keep my day streak rolling, and they offer more points to add up.

Thanks for chatting with me and assisting me with my project.

:bow:


#13

the solution is so cool. :bulb:

I want to believe that I might have figured it out.
The format is very similar to #65, using the [ ] .
I believe Im ready for what #67 is going to toss my direction

I was looking at this for a few minutes
https://wiki.python.org/moin/ForLoop

but, I did end up opening up your reveal code.
I saw the beauty in the simplicity. :milky_way: :leaves:

Very grateful for your help. Thank you @gaurangtandon
I've only just recently begun to use the forum.
I usually just search through the posts,
but this series of exercises didnt have much discussion attached to them.
Also big thanks to @ionatan , @zainabrawat , & @one.two.smilez
I've been gathering up some good Codecademy inspiration recently.

Cheers !
DB


#14

I used the exact same code in the next exercise and it gave the green light.

Glad I posted the topic.
Everyone out there at Codecademy helped me out a lot !
Thank you !

DB

:v:

:vulcan: