Jump to content


Photo

Chapter 7 Page 225


  • Please log in to reply
3 replies to this topic

#1 J Coleman

J Coleman

    New Member

  • Members
  • Pip
  • 1 posts

Posted 17 May 2013 - 12:33 PM

on page 224, in put_to_store, we get each athlete's data into an AthleteList from get_coach_data, then we store that data in the dictionary. Like this:

ath = get_coach_data(each_file)
all_athletes[ath.name] = ath.

that makes total sense to me. However, on page 225, there is this for statement:

for each_athlete in data:
print (data[each_athlete].name + ' ' + data[each_athlete].dob)

we are not "indexing" into the dictionary with a name like on page 224, it seems, we are using an entire AthleteList. That's my confusion.

Is it that specifying each_athlete somehow automatically takes the first attribute?

JB

#2 paulbarry

paulbarry

    Advanced Member

  • O'Reilly Author
  • PipPipPipPipPipPipPipPip
  • 307 posts

Posted 30 May 2013 - 11:55 PM

In the first snippet of code, we are getting the data from the file, then using the "name" part of the athlete object to act as the key in the dictionary.

In the second snippet, the for loop takes the entire dictionary (data) and - on each iteration - returns each of the keys, assigning them one-at-a-time to the "each_athlete" variable. This is then used as a key to index into the data dictionary and pull out the data that we are interested in (the "name" and the "dob"). The for loop is smart enough to know that it is working on a dictionary, so it grabs each key each time around the loop. Once you have the key, you can get at the entire object by putting the key between the square brackets.

Hope this helps.

--Paul.

#3 shamifd

shamifd

    New Member

  • Members
  • Pip
  • 3 posts

Posted 25 July 2013 - 12:30 AM

On page 225, I try this snippet:

for each_athlete in data:
print(data[each_athlete].name + ' ' + data[each_athlete].dob)

and get this:

Traceback (most recent call last):
File "<pyshell#163>", line 1, in <module>
for each_athlete in data:
TypeError: 'NoneType' object is not iterable

#4 shamifd

shamifd

    New Member

  • Members
  • Pip
  • 3 posts

Posted 25 July 2013 - 06:00 AM

On page 225, I try this snippet:

for each_athlete in data:
print(data[each_athlete].name + ' ' + data[each_athlete].dob)

and get this:

Traceback (most recent call last):
File "<pyshell#163>", line 1, in <module>
for each_athlete in data:
TypeError: 'NoneType' object is not iterable


It's ok now. There is a type error in the athletemodel.py!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users