Python Program 3.5 - Champion Counter Create a program that reads a text file th
ID: 3817822 • Letter: P
Question
Python Program 3.5 -
Champion Counter Create a program that reads a text file that contains a list of FIFA World Cup champions and determines, by the display shown below, the country that has won the most championships. Use the world_cup_champions.txt file provided for you in this Module for your program.
When the program starts, it should read the text file and use a dictionary to store the required data using the name of each country that has won the World Cup as the key.
The program should display the countries alphabetically.
Text File Year,Country,Coach,Captain
1930,Uruguay,Alberto Suppici,José Nasazzi
1934,Italy,Vittorio Pozzo,Gianpiero Combi
1938,Italy,Vittorio Pozzo,Giuseppe Meazza
1950,Uruguay,Juan López,Obdulio Varela
1954,Germany,Sepp Herberger,Fritz Walter
1958,Brazil,Vicente Feola,Hilderaldo Bellini
1962,Brazil,Aymoré Moreira,Mauro Ramos
1966,England,Alf Ramsey,Bobby Moore
1970,Brazil,Mário Zagallo,Carlos Alberto
1974,Germany,Helmut Schön,Franz Beckenbauer
1978,Argentina,César Luis Menotti,Daniel Passarella
1982,Italy,Enzo Bearzot,Dino Zoff 1986,
Argentina,Carlos Bilardo,Diego Maradona
1990,Germany,Franz Beckenbauer,Lothar MatthÃus
1994,Brazil,Carlos Alberto Parreira,Dunga
1998,France,Aimé Jacquet,Didier Deschamps
2002,Brazil,Luiz Felipe Scolari,Cafu
2006,Italy,Marcello Lippi,Fabio Cannavaro
2010,Spain,Vicente del Bosque,Iker Casillas
2014,Germany,Joachim Löw,Philipp Lahm
Explanation / Answer
'''Opening and reading the contents of a file'''
f2 = open("world_cup_champions.txt","r")
dict_values ={}
temp_list = []
tmp_list2 = []
'''reading the values in file and appending it to a temp list'''
for l in f2:
temp_list.append(l.strip())
''' Removing the first line coz it consists of header'''
temp_list = temp_list[1:]
'''Adding country values with the number of world cup wins to a dictionary'''
for val in temp_list:
tmp_val = val.split(',')
if tmp_val[1] not in dict_values:
dict_values[tmp_val[1]] = 1
else:
dict_values[tmp_val[1]] += 1
''' Appending the obtained results as list of lists to a temp list'''
for key,value in dict_values.items():
tmp_list2.append([key, value])
'''Sorting the list based on the first element i.e based on the name of the country'''
tmp_list2.sort(key=lambda x: x[0])
print(" Country " + " No.Of Worldcups ")
for val in tmp_list2:
print(" " + val[0] + " " + str(val[1]))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.