I want to create a game that displays a country and asks the user to enter the capital for the country displayed. After the user enters the capital, it will display another country and ask for the capital, and repeat the process until the user has answered the capital for ten countries. Then, display the users score at the end of the game. For each capital the user enters correctly, I want to award 5 points.
This is what I have done so far
import csv
import pandas
import random
african_countries = open("african_countries.csv", "r")
rd = csv.reader(african_countries)
def main():
setupGame()
playGame()
def setupGame():
global countries, capitals, correct, incorrect, used
correct = 0
incorrect = 0
used = [False] * 55
countries = setupCountriesList()
capitals = setupCapitalsDictionary()
print("\nCOUNTRIES AND CAPITALS QUIZ!")
def playGame():
global correct, incorrect, used
guess = ""
while guess.lower() != "quit":
idx = random.randint(0, 9)
while used[idx]:
idx = random.randint(0, 9) #To generate a new idx if already used
used[idx] = True
allTrue = True #check to see if all used is True
for i in range(0,55):
if used[i] == False:
allTrue = False
if allTrue:
used = [False] * 55 #To reset all used to false
country = countries[idx]
capital = capitals[country]
guess = input("What is the capital of " + country.upper() + "? (enter 'quit' to end)>> ")
if guess.lower() =="quit":
print("THANKS FOR PLAYING...You got {0} of {1} correct.\n".format(correct, (correct + incorrect)))
break
elif guess.lower() == capital.lower():
print("CORRECT! {0} is the capital of {1}".format(capital, country))
correct += 5
else:
print("SORRY... The capital of {0} is {1}".format(country, capital))
incorrect += 5
print("YOUR SCORE: You have gotten {0} of {1} correct \n".format(correct, (correct + incorrect)))
def setupCountriesList():
countries = []
for row in rd:
countries.append(row[0])
return countries
def setupCapitalsDictionary():
capitals = {}
for row in rd:
k, v = row
capitals[k] = v
return capitals
main()
But I got this error:
COUNTRIES AND CAPITALS QUIZ!
Traceback (most recent call last):
File "c:/Users/Gideon Markus/Desktop/Cyhermes/Week 4/Project 3/Python/trial.py", line 61, in <module>
main()
File "c:/Users/Gideon Markus/Desktop/Cyhermes/Week 4/Project 3/Python/trial.py", line 10, in main
playGame()
File "c:/Users/Gideon Markus/Desktop/Cyhermes/Week 4/Project 3/Python/trial.py", line 35, in playGame
capital = capitals[country]
KeyError: 'The Republic of Cabo Verde'
PS C:\Users\Gideon Markus\Desktop\Cyhermes\Week 4\Project 3\Python>
Aucun commentaire:
Enregistrer un commentaire