#3: the 'In' Operator


#1

CA gives an OK on this code:

my_dict = {
    "Name": "boy",
    "Status": "owned",
    "Punished": True
}

print my_dict.keys()
print my_dict.values()

for key in my_dict:
    print key, my_dict[key]

...but not on this code:

my_dict = {
    "Name": "boy",
    "Status": "owned",
    "Punished": True
}

print my_dict.keys()
print my_dict.values()

for key in my_dict:
    print key, my_dict[key],

...saying: Oops, try again. It looks like you did not print out every key in my_dict.

The addition of the comma on the last line is supposed to keep output all on the same line. What am I missing here?


#2

there is one tiny flaw, in this line:

print key, my_dict[key],

there is a , at the end, and it shouldn't be there. Then you should be good, also for next time, use one of the following instructions to keep the indention intact:

select your code and press ctrl + shift + c (or cmd + shift + c if you use a mac)

if this instructions are unclear, you can also insert 3 backticks before and after your code, like so:

```
<p>visible</p>
```

the backtick is located above the tab key on your keyboard. I edited your code, so you can edit it to see what i did


#3

Thanks...
So even tho the instruction mentioned using a comma to keep output on the same line, we're not supposed to use the comma?


#4

let me explain, you have this:

print key, my_dict[key],

you should use a comma between key and my_dict[key] to keep it on the same line, but no comma after my_dict[key]


#5

I get what you're saying, and I thank you, but this is the example that is used before we are asked to try our own code:
for number in range(5):
print number,

d = { "name": "Eric", "age": 26 }
for key in d:
print key, d[key],

for letter in "Eric":
print letter, # note the comma!

Using the example, when I attempt to use the trailing comma, I get an error. Of course, when I delete the comma, CA says OK.
I know this is a small detail and not really a big deal, but combine this and many other 'small confusions' with the daunting task of learning a programming language becomes off-putting to say the least.


#6

Weird, it could be a problem in codecademy SCT (submission correctness test), i think you should continue to use the trailing comma, but it causes problems in this exercise


#7

OK... Again, a really small detail about which one has to be willing to be flexible enough to continue to learn, but as someone who has struggled more than now with learning, I can understand how someone new, and not necessarily tech-friendly, can easily resort to procrastination! I should tell my story to the greater community. I know I'm not alone!


#8

Yea, there is a introduction topic, where you can introduce yourself, i recommend that


#9

anotherdirection, thanks for posting this. I had the same question and didn't find a definitive answer why the note in the instruction directly contradicted how the tester parsed the answer. I'm going to chalk this up to an oversight in the test verification.


#11

there is nothing wrong i tried it and works pretty well. example code works good , but own code with comma give error as long as you do not add another print line underneath

my_dict = {
    'name': str(raw_input('your name:')),
    'age': int(raw_input('your age:'))
    }
for key in my_dict:
    print key, my_dict[key],
print "Hello"

comma after [key] means there must be another line of code to print out. you can check that with and without comma and adding print 'hello' to comment with hasch.

thanks for question i learned smth new and understand a little bit more how python works :slight_smile:


#12