11.4 For The Record Confusion


#1



So, I have the code that is correct. What I am wondering is how to I determine what to put in the spots like after the for and after print.

I was looking at the past lessons, and either the information is not there or I'm being dumb and not looking in the right place.

Help would be appreciated. Thanks!


students = [lloyd, alice, tyler]

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]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

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


#2

Could you post the error you are receiving to help me narrow down to where to search for your error?


#3

AAHHAA!! never mind, I figured out your error.
Python looks at the first line: "hey, ok, we got this students list dr_zomboss is creating. LETS JUMP IN!!! Hmmm... WHAT THE??? what the heck is lloyd, i don't know what that is, that why im gonna stop in my tracks, and throw a variable undefined error."

Now your probably asking yourself, "how do i fix this error". Well, we created the students list, so that the dictionaries can be looped through easier. However, for python, those dictionaries aren't even defined yet. So how do we create that list, so that lloyd and his pals in his students list are defined already, so that they can be used in that loob below their respective dictionaries?

If you still can't figure out, just ask, but I think it will be more satisfactory for you if you figure out at least part of the error based off of my hints :slight_smile: HAPPY CODING !!!!!!


#4

Yeah, I realised that after I posted it. :stuck_out_tongue:

I want to know how to know what to put in the for loop. I don't know whether to put students or key or item after the for and how that corresponds to your existing lists and you code in the for loop. Sorry if that long sentence confuses you

ex:

for ???? in ????
# in this case,
print ????["name or hw or whatever"]

#5

Its actually much more intuitive than you think. The point of a loop is that repeats executing certain lines of code over and over again, until the list or dictionary it is looping runs out (There are other things the loop can go through, like the range function, but I don't think you learned that yet). Pretty much, Python is saying "for every element inside this list of students, i want to print it, so I start with element 0, print it, then element 1, print it, etc." So in this case, "for every student in the students list, I wanna print their name, Hw grade, tests grade, and quizzes grade." So when you are writing loops, think of that sentence: for every element/item/key (whatever you want to call it) inside this list or dictionary, i wanna do this stuff to it. So, the general sytnax would be:

for element in my_list:
    print element #this will print every element in the list, and the loop will exit once every element is printed

#6

can you clarify on that. Perhaps you meant to say it repeats the execution of certain lines of code?


#7

I'm getting the error "Did you print out each student's name? It looks like "Lloyd" wasn't printed" however the names are printed in the console.

Here's my code:

students = [lloyd, alice, tyler]
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]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
for element in students:
print element


#8

i was getting that same "lloyd wasn't printed" error message.

finally, this code worked for me.

students = [lloyd, alice, tyler]

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]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}

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


#9

thanks a lot!! You just get me out of the ■■■■.


#10

Nice catch, yeah, ur right. Good point.


#11

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]
}
tyler = {
    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

students = ["Lloyd","Alice","Tyler"]
for student in students:
    print student["name"]
    print student["homework"]
    print student["quizzes"]
    print student["tests"]

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

gives me an error:
TypeError: string indices must be integers, not str. And also the same error appears if i use code below

for student in students:
    print "name: %s" %(student["name"])
    print "homework: %s" %(student["homework"])
    print "quizzes: %s" %(student["quizzes"])
    print "tests: %s" %(student["tests"])

#12

just follow this:
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]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd, alice, tyler]
for item in students:
print item['name']
print item['homework']
print item['quizzes']
print item['tests']


#13

Follow this
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]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd, alice, tyler]
for student in students:
print student['name']
print student['homework']
print student['quizzes']
print student['tests']


#14

were you able to solve your problam, I am getting same error


#15

students = [lloyd, alice, tyler]


#16

students = [lloyd, alice, tyler]
for student in students:
print student["name"]
print student["homework"]
print student["quizzes"]
print student["tests"]

Make sure to add the students dict at the top.
Also, DONT FORGET TO INDENT THE PRINT FUNCTIONS. Idk to do that here.


#17

[quote="arcninja56229, post:11, topic:33053"]

thanks chenrui.

For others who do not see:
Do not use quotation marks and keep the same caps.

students = [lloyd, alice, tyler]
NOT students = ["Lloyd","Alice","Tyler"]


#18