-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfootballTriviaGame.py
101 lines (63 loc) · 2.7 KB
/
footballTriviaGame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Architecture taken from Kenny's pokeGame example
import urllib.request # request library
import json # json parsing
import random
# import pprint # pretty print library, useful for printing jsons
BASE_API_ADDRESS = "http://api.football-data.org/v1/"
GET_ROUTE_COMP = "competitions/"
GET_ROUTE_TEAM = "teams/"
def getJSON(param, route):
# urllib.request.Request makes a request object to prepare a get request
requestParam = urllib.request.Request(BASE_API_ADDRESS + route + param)
# print("URI for request:" + requestParam);
# add our header hack
# requestParam.add_header('User-Agent', HEADER_HACK)
# gives a byte endoed string
dataBytes = urllib.request.urlopen(requestParam).read()
# we decode that in preparation for json parsing
# techically not necessary (since json has a way to decode) but we did this for debugging
dataParam = str(dataBytes, 'utf-8')
# turns a jsonString into a dictionary
result = json.loads(dataParam)
# print(str(result))
return result
def getTeamJSON(teamName):
teamSearchParam = "?name=" + teamName
teamJ = getJSON(teamSearchParam, GET_ROUTE_TEAM)
teamID = teamJ["teams"][0]["id"]
return teamID
def getPlayerList(teamID):
param = "%s/players" % teamID
playerList = getJSON(param, GET_ROUTE_TEAM)["players"]
playerNumberList = list()
for playerDict in playerList:
playerName, playerNumber = playerDict["name"], playerDict["jerseyNumber"]
playerNumberList.append((playerName, playerNumber))
playerNumberList.sort(key=lambda num: num[1])
return playerNumberList
def main():
active = True
print("Hi! This is a game where you test your knowledge of players of different teams")
teamName = (input(
"name of Team you want to get quizzed on? (type quit or Ctrl_C to quit)\n")).lower()
# did it just in case someone presses an arrow key and can't take it back
teamName = teamName.strip()
# teamName = teamName.replace(" ", "-")
teamName = teamName.replace(" ", "%20")
teamID = getTeamJSON(teamName)
playerNumberList = getPlayerList(teamID)
while (active):
try:
rand = random.randint(0, len(playerNumberList) - 1)
randPlayer = playerNumberList[rand]
guess = int(input("What is %s's jersey Number?" % randPlayer[0]))
if (guess == randPlayer[1]):
print("Nice you got it!")
else:
print("nope you were wrong!")
playerNumberList.remove(randPlayer)
if (len(playerNumberList) == 0):
break
except urllib.error.HTTPError:
print("OOPS probably misspelled something, try again!")
main()