Why Doesn't This Code Pass the Test?


#1

Why doesn’t this code pass the test?

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 '''%s
%s
%s
%s
  ''' % (student['name'], student['homework'], student['quizzes'], student['tests'])

It outputs:

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]
  
Tyler
[0.0, 87.0, 75.0, 22.0]
[0.0, 75.0, 78.0]
[100.0, 100.0]

FAQ: Learn Python: Student Becomes the Teacher - For the Record
#2

the exercise very likely validates the presence of multiple print keywords, which is why this is not working


#3

Is is objectively better to do it that way, or is it just an oversight in the code-checking system?


#4

Better is always a difficult term, given it consist of multiple factors. For example you could also do:

for student in students:
  print '%s \n%s \n%s \n%s' % (
      student['name'], 
      student['homework'], 
      student['quizzes'], 
      student['tests'])

now i made the string smaller and gave more space to the variables. Is this “better” then your solution? Its difficult to say. So many ways to solve the problem, but lets quote knut:

premature optimization is the root of all evil (or at least most of it) in programming

worrying too much on small detail will waste more time then you gain profit from it.

If you don’t know who knut, he wrote a book series named the art of computer programming, of which bill gates said: If you can read those books, you can send me your resume (which very likely means you are almost instantly hired)