I have a .txt file with all goals scored in the 2018 world cup, there are 169 lines in this file structured as:
playername;country;minute;
EG:
Gazinsky;Russia;12;
Cheryshev;Russia;43;
Cheryshev;Russia;91;
Dzyuba;Russia;71;
Golovin;Russia;94;
Gimenez;Uruguay;89;
Cheryshev;Russia;59;
Dzyuba;Russia;62;
OG;Russia;47;
Salah;Egypt;73;
(The minute is the minute the goal was scored)
I want to create a Quiz() function that iterates over each line in this .txt file and adds each country to a python dictionary, and adds the number of goals scored for each country: for example
{Russia: 11},
{England: 12},
{Croatia: 14},
etc.
(the number of goals scored is simply the number of times they appear in the .txt file.)
I would then choose two random countries and ask the user to guess which country scored more goals in the world cup.
Here is the code I have so far:
import random
def Quiz():
# randomly pick two countries from the world cup
# ask the user to guess which country scored more goals in 2018 world cup
countries = []
file = open("Goals.txt", "r")
for line in file:
data = line.split(";")
country = data[1]
countries.append(country)
file.close()
# Randomly generating the countries
countries = list(dict.fromkeys(countries))
country_1 = random.choice(countries)
country_2 = random.choice(countries)
if country_1 == country_2:
country_2 = random.random(countries)
print("Which country scored more goals in the 2018 world cup?")
print(country_1)
print("OR")
print(country_2)
userInput = input("Answer: ").title()
I need help with creating this dictionary, and then using the users input to work out if they were correct or not. Any help massively appreciated!