From the course: Level Up: Advanced Python

Get Olympic medals - Python Tutorial

From the course: Level Up: Advanced Python

Get Olympic medals

(8-bit music) - [Lecturer] In this challenge, we're going to retrieve medal results from the Olympic Games. I really like the Olympic Games, and the Chariots of Fire is one of my favorite Olympic-related movies. In the 1924 Olympic Games, Eric Liddell, because of his religious convictions, ends up running the 400 meters, instead of his preferred hundred meter sprint. Now, I've read in data from the Olympics dataset, which includes medals won from 1896 to 2008, and it's stored in the variables "olympics," and each line in the dataset corresponds to a medal winner. In this challenge, create "medals," which is a list of medal nametuples. Then go ahead, and complete the function "get_medals", so that anyone using it can enter one or more keyword arguments, and a list of medal nametuples is returned. So, let me give you an example. Now, we know that Eric Liddell competed in the 1924 Olympic Games, and participated in the 400 meter event. So how can we check if he competed in any other event in the 1924 Paris Games? Well, all I need to do is to remove the "Event" keyword argument. And you can see, that Eric Henry Liddell also took part in the 200 meter event, and received a bronze medal. Now, did he compete in any other Olympic games? While we know from the film, that this was his only Olympic games, and he later went to China as a missionary. So, let's go ahead and confirm that. So, I'm just going to clear my screen. And all I need to do, is to get rid of the "Edition". Now our function confirms and only returns details for the 1924 Olympics. Now, I should be able to enter the details into the "get_medals" function for any Olympic athlete, and for any of the games or events between 1896 and 2008. So go ahead and update "medals.py", and pause the video here, and I'll show you the solution I came up with. (8-bit music) Let's look at the "medals" list first. We go through each of the lines, skipping the first line, as this has the column headings. We then split each line by semicolons. This gives us a list, which we can then unpack into the "medal" namedtuples. Now, let's head over to the "get_medals" function. We accept, as input, the keyword arguments, and we then work our way through all of the medals in the list "medals," that we created a minute ago. And then we only want to return "medals", where the named attribute for "medal", so, that's the key in the keyword argument, is equal to the value. And we can use the "getattr," because we don't know in advance which attribute will be passed in. So let's go ahead and check, if this passes all our tests. "Pytest", test, "test_medal". And this message confirms, this passes all our tests. Now, my solution is just one way of solving this problem. Go ahead, and share your solution in the Q and A section. I'm really keen to see how you solved this code challenge.

Contents