More lists and loops solution

Overview
Points to remember:
• Name your submissions as hw4_part1.py and hw4_part2.py, all lower case. Otherwise, we will not be able to locate and execute your submissions properly.
• As in Homework#3, expect diﬀerent input cases and input ﬁles in the submission server. Your program must work for all possible inputs, not just the ones we test for.
• Remember to print any input you read immediately as we have done in previous homewroks.
• We will grade you on correct program as well as good program structure. We will also continue to check for “excessive collaboration”.

Part 1: World cup strikes again
Your solution for this part must be called hw4_part1.py. Only submit this ﬁle.
You will use the input module called hw4_util.py to read a ﬁle all_games.txt containing all matches in the World Cup. Read this ﬁle into a list of lists in which each item in the list is a diﬀerent game.
import hw4_util results = hw4_util.read_games('all_games.txt') print results[-1]
For example, given ['Brazil', 3, 'Croatia', 1, 'Brazil'], we know that the score of the Brazil-Croatia game was 3-1, and Brazil won.
Similarly, ['Netherlands', 0, 'Costa Rica', 0, 'Netherlands'] means that the score of the Netherlands-Costa Rica was 0-0, but Netherlands won (presumably by penalty kicks).
Finally, ['Ecuador', 0, 'France', 0, 'Draw'] means that this game was a draw. No one won.
Write a program to do the following:
• Read the name of a country (upper/lower case should not matter). • Find and print all the games this country played. Print the total number of games played, wins, lose, draws, goals for and against.
• Print whether the country played in the round of 16, quarter ﬁnals, semi ﬁnals or the ﬁnal. The game number will tell the story. First 48 games are group matches, games 49-56 are round of 16, games 57-60 are quarter ﬁnals, 61-62 are semi-ﬁnals, 63rd game is the third place playoﬀ and 64th game is the ﬁnal. Print the highest achievement for the team (only the group they played in).
• Each game is printed as team 1 name (rjust(24)), team 1 score (rjust(4)), team 2 score (ljust(4)) and team 2 name (ljust(24)). All team stats are printed with ljust(6). • (Bonus +5 points) Print the ﬁnal standing of this team if it is one of ﬁrst, second, third or fourth place.
Here is a possible run of the program:
Please enter a country == Algeria Algeria All games: Belgium 2-1 Algeria Winner:Belgium Korea Republic 2-4 Algeria Winner:Algeria Algeria 1-1 Russia Winner:Draw Germany 2-1 Algeria Winner:Germany Scores: Games Win Lose Draw GF GA 4 1 2 1 7 7 The highest achievement in 2014 World cup was round of 16
Please enter a country == England England All games: England 1-2 Italy Winner:Italy Uruguay 2-1 England Winner:Uruguay Costa Rica 0-0 England Winner:Draw Scores: Games Win Lose Draw GF GA 3 0 2 1 2 4 The highest achievement in 2014 World cup was group games
Please enter a country == Germany Germany All games: Germany 4-0 Portugal Winner:Germany Germany 2-2 Ghana Winner:Draw USA 0-1 Germany Winner:Germany Germany 2-1 Algeria Winner:Germany France 0-1 Germany Winner:Germany Brazil 1-7 Germany Winner:Germany Germany 1-0 Argentina Winner:Germany Scores: Games Win Lose Draw GF GA 7 6 0 1 18 4 The highest achievement in 2014 World cup was finals This country placed in the first place
Please enter a country == Turkey Turkey

All games: Scores: Games Win Lose Draw GF GA 0 0 0 0 0 0 The highest achievement in 2014 World cup was not making it to the tournament
Here are some hints to get you started. Clearly, you will need a loop to go through all the games one by one, and check whether the current team is actually the team we are searching for. One you ﬁnd it, print it out. Test your code!
Now, add some variables to count the total number of wins, draws and losses. Remember the team name can appear as the ﬁrst or the second team.
When you are done with all this, the ﬁnal step is looking at the team standing. Keep track of the highest index in the list in which you ﬁnd a game for this team. Using this index, you can ﬁgure out where this team has played.

Part 2: Olympic Medals

Your solution for this part must be called hw4_part2.py. Only submit this ﬁle.
You will use the input module called hw4_util.py to read a ﬁle containing medal standings of diﬀerent countries. We have two ﬁles, one for 2010 and one for the current standings in 2014. For each country, you have medal counts for: gold, silver, bronze, total medals
If the country has won no medals, it is not in this list. Here is an example use:
import hw4_util medals2010 = hw4_util.read_medals('medals2010.txt') print medals2010
Write a program that:
• Reads the medal counts for 2010 and 2014 using this module.
• Asks the user for the name of a country
• Prints the percentage of all gold, silver, bronze and total medals the input country and United States got in 2010 and in 2014.
• If the input country has no medals, you print nothing.
• Format your output so that the country column is 14 characters long and all the other columns are 8 characters long. The ﬁrst two columns are left justiﬁed as before, but the last 4 columns are right justiﬁed (use rjust).
Example output for diﬀerent runs of this program shown below.

Enter the name of a country == Russia Russia Year Country Gold Silver Bronze Total ====================================================== 2010 Russia 3% 5% 8% 5% 2010 United States 10% 17% 15% 14%
2014 Russia 10% 11% 7% 9% 2014 United States 10% 8% 12% 10%
Enter the name of a country == Ukraine Ukraine Year Country Gold Silver Bronze Total ====================================================== 2010 United States 10% 17% 15% 14%
2014 Ukraine 0% 0% 1% 0% 2014 United States 10% 8% 12% 10%
As Ukraine won no medals in 2010, so the programs prints nothing for this country.
Here is a bit of help on how to organize your code. You do not have to use these suggestions, but if you do, your code will be considerably shorter and easier to debug.
• First ﬁnd the medal totals in each of the four categories (gold, silver, bronze, total) for 2010 and 2014. Store them in a list e.g. totals2010= [0,0,0,0]. You need to go through each list of medals one by one using a loop.
• Write a function such as: print_percentage(year, medals, totals, country) which goes through a list of medals for an input country using a loop and if it ﬁnds the given country in the list, it prints the percentage information using the totals from the second list.
• Once you have all this in place, all you need to do is to call this function for the searched country and US for years 2010 and 2014. For example:
print_percentage('2010', medals2010, totals2010, country) print_percentage('2010', medals2010, totals2010, 'United States')
Part 3: A bit of repetition!
Your solution for this part must be called hw4_part3.py. You cannot use any modules for this part.
Ok, this ﬁnal part is simple and challenging at the same time. It is only worth 10 points and builds on monday’s lecture on string slicing (October 6). My solution is 15 lines of code. We will replicate every horror movie cliche here.

Write a program that: