For the record


#1

loyd = {
"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:
for name in students:
print student["name"]
for homework in students:
print student["homework"]
for quizzes in students:
print student["quizzes"]
for test in students:
print student["test"]

Here is my code, for some reason it prints 3 times what I am asking it to print. Am I doing something wrong?


#2

Each time your loop visits a student, you also loop through the students.

So for 3 students you are going to loop 9 times, 16 for 4, 25 for 5, 36 for 6, and so on


#3

but is it normal to be that way? or is it possible to make it doing once?


#4

Normal? That depends on what you mean. Python is merely following your instructions.

Refer to what you want to do.
Loop through the students, printing out information about each one.

How many loops are mentioned in that description? One. How many loops are there in your code?


#5

I dont really understand what are you trying to say, maybe I should keep practicing and see what comes next. I was wondering because that happened to me before and I was wondering why.


#6

The instructions describe looping through the students and doing a few actions for each one. Therefore, there should be one loop, and any more than that is a bug - there is nothing else to repeat


#7

you have to write for loop just once like this :slight_smile:

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


#8

I see, thank you. Now is printing once.


#9

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