Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYuqing Lu
7,032 PointsNeed Help!!!
Hi, anyone can suggest how do I correct this code. In pyCharm, this code can only output one result instead of a list, for example, if some_player is a instance of Player(), print(some_player.last_name) can only get "Steward Pinchback" but not the whole list of all last_names. and of course, it won't pass the challenge. however I am more concerned with how to make the code to work as I expect other than passing a test. thank you!!!
import re
string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''
players = re.search('''
^(?P<last_name>[\w\s]+)
,\s
(?P<first_name>[\w\s]+)
:\s
(?P<score>\d{2})$
''', string, re.X|re.M)
class Player:
line = re.compile('''
^(?P<last_name>[\w\s]+)
,\s
(?P<first_name>[\w\s]+)
:\s
(?P<score>\d{2})$
''', re.X | re.M)
def __init__(self, string):
for match in self.line.finditer(string):
self.last_name = match.group('last_name')
self.first_name = match.group('first_name')
self.score = match.group('score')
1 Answer
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 PointsWithout looking at the challenge, but just your question, you say you only get "Steward Pinchback" back, which is the last name in the list. This is because for each iteration of the for loop, the self.last_name, first and score is set, overriding the previous name that was set. If you want it to return a list, you need to create a list, at the moment you have only variables.
Yuqing Lu
7,032 PointsYuqing Lu
7,032 PointsHi, Christopher, Thank you again, you really pointed it out!