4 For the Record, what if using 'x','all','each' instead of student


#1

I got the answer...
Interestingly, in the line of 'for student in students:', if student is replaced by 'x','all' or 'each', i.e.' 'for x in students:', Tyler's details will be printed 3 times instead of error message..
Anyone knows what is the logic behind?


#2

You can use whatever name for the index (index variable) and you'll still get the same results if you are consistent.
For example:
Let's say you have a list of fruits:

fruits = ["apple", "banana", "orange"]

And you want to print all the fruits out. You use a for loop:

for fruit in fruits:
    print fruit
>> "apple"
>> "banana"
>> "orange"

Similarly, if you use x, all, or each, you'll still get the same result:

for x in fruits:
    print x
>> "apple"
>> "banana"
>> "orange"

Python is smart enough to know that the index variable in the loop refers to the indexes of the input.


#3

Thank you!
What I meant is if only student is replaced by any index variable, while the rest is still left alone:
for x in students:
print student['name']
print student['homework']
print student['quizzes']
print student['tests']
i would have expected an error message, instead it prints:
Tyler
[0.0, 87.0, 75.0, 22.0]
[0.0, 75.0, 78.0]
[100.0, 100.0]
Tyler
[0.0, 87.0, 75.0, 22.0]
[0.0, 75.0, 78.0]
[100.0, 100.0]
Tyler
[0.0, 87.0, 75.0, 22.0]
[0.0, 75.0, 78.0]
[100.0, 100.0]
None

So my question is why Tyler's particulars are printed out 3 times?


#4

If you are changing the index from student to x, the rest will have to follow:

for x in students:    
    print x['name']   
    print x['homework']    
    print x['quizzes']    
    print x['tests']

It's the same idea for my examples. Notice I didn't do

for x in fruits:
    print fruit

or

for fruit in fruits:
    print x

#5

Yes, I know what you meant in you first reply.
The correct answer will be printed, if 'student' in all statements in replaced by any index variables, be it 'x','each' or 'all.
What I wanna know is if you just replace the first line, like what i have showed in my second post, I would not have expected Tyler being printed three times. My question is why 'Tyler' but not anyone else, and how does Python make a choice?

Thank you very much!


#6

Weird. That shouldn't happen. I get an error message stating that student is not defined (which is probably what you expected to get).
I was thinking it's probably that Codecademy didn't properly refresh to evaluate your current code (So you need to do a hard refresh yourself).

I can't reproduce your result so I have no clue sorry :confused:


#7

you are very right! i reset the code and replace only the index variable, i get this

"Traceback (most recent call last):
File "python", line 23, in
NameError: name 'student' is not defined"

However if then i do my little 'trick', " Tyler" again gets printed out 3x. :stuck_out_tongue:


#8

Trick? So it's not just replacing only the index variable? I still cannot reproduce your tyler problem.


#9

This may not give any meaning in learning python, not sure if it is a bug...
I figure that it always prints out the last one...sorry, very long....

tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}

students=[tyler, lloyd, alice]

for x in students:
print x["name"]
print x["homework"]
print x["quizzes"]
print x["tests"]

THIS TRANSLATES TO:

Tyler
[0.0, 87.0, 75.0, 22.0]
[0.0, 75.0, 78.0]
[100.0, 100.0]
Lloyd
[90.0, 97.0, 75.0, 92.0]
[88.0, 40.0, 94.0]
[75.0, 90.0]
Alice
[100.0, 92.0, 98.0, 100.0]
[82.0, 83.0, 91.0]
[89.0, 97.0]
None

================================

tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}

students=[tyler, lloyd, alice]

for m in students:
print x["name"]
print x["homework"]
print x["quizzes"]
print x["tests"]

THIS TRANSLATES TO:

Alice
[100.0, 92.0, 98.0, 100.0]
[82.0, 83.0, 91.0]
[89.0, 97.0]
Alice
[100.0, 92.0, 98.0, 100.0]
[82.0, 83.0, 91.0]
[89.0, 97.0]
Alice
[100.0, 92.0, 98.0, 100.0]
[82.0, 83.0, 91.0]
[89.0, 97.0]
None


#10

I think it's a bug. I was able to reproduce it now in my environment but when I moved it somewhere else to reproduce, it gives an error message. So I think it's a Codecademy thing.

I believe it lies in the fact that you did before changing it to m without hard refreshing after running it. I think Codecademy retains the previous submission and hence then you changed x to m you get 3 Alices. It is because Alice was the last one to be printed out and since there were 3 items in your list and you're not using the correct index (m), Alice gets printed 3 times as Alice was the last index and therefore is the one stored in x currently.

for x in students:
    print x["name"]
    print x["homework"]
    print x["quizzes"]
    print x["tests"]

You can see it in this link: https://repl.it/GTJ0/0
I've basically put

for x in students:
    print x["name"]
    print x["homework"]
    print x["quizzes"]
    print x["tests"]

and

for m in students:
    print x["name"]
    print x["homework"]
    print x["quizzes"]
    print x["tests"]

together so you can see why 3 Alices gets printed in the end.

TL:DR It's a Codecademy issue of not being able to hard refresh some parts of the code. You can modify it and hard refresh it to get the error message. Otherwise just changing it and hitting Save&Submit will get you the 3 Alices/Tylers problem.


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.