generated from Code-Institute-Org/python-essentials-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
270 lines (230 loc) · 8.38 KB
/
run.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
"""
Import modules for use in app
"""
import gspread
from google.oauth2.service_account import Credentials
from tabulate import tabulate
SCOPE = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
CREDS = Credentials.from_service_account_file('creds.json')
SCOPED_CREDS = CREDS.with_scopes(SCOPE)
GSPREAD_CLIENT = gspread.authorize(SCOPED_CREDS)
SHEET = GSPREAD_CLIENT.open('python_quiz_questions')
PLYR_SCORE = 0
"""
Title screen
"""
print("""\033[1;32;40m
██████ ██████ ██████ ███████
██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ █████
██ ██ ██ ██ ██ ██
██████ ██████ ██████ ███████
██████ ██ ██ ██ ███████
██ ██ ██ ██ ██ ███
██ ██ ██ ██ ██ ███
██ ▄▄ ██ ██ ██ ██ ███
██████ ██████ ██ ███████
\033[0;37;48m\n""")
CHOS_CAT = str("1")
PLYR_SCORE = 0
"""
Set the two global values used throughout our quiz
"""
def instructions():
"""
Instructions function
"""
print("\033[1;32;40m WELCOME TO THE QUIZ !\033[0;37;48m\n")
print(" In the main menu you can select a quiz category")
print(" A question list will then be built from an external spreadsheet")
print(" You will then answer each question by selecting A B or C\n")
print(" NOTE - YOU MUST ANSWER BY TYPING THE LETTER A, B OR C")
print(" No other input is allowed...\n")
print(" At game over you save your score and choose to play again!")
print("\033[1;32;40m VIEW THE SCORETABLE AT MAIN MENU!\033[0;37;48m")
print("______________________________________________________________\n")
start_menu()
def scoretable():
"""
Scoretable function
"""
print(" CHECK OUT OUR HIGH SCORE TABLE BELOW\n")
get_scores = SHEET.worksheet('scores').get_all_values()
score_list = get_scores
print(tabulate(score_list, headers="firstrow"))
print("______________________________________________________________\n")
validated = 0
while validated < 1:
cont = input(" PRESS A TO CONTINUE\n ")
print(" \n")
if cont.upper() == "A":
validated += 1
start_menu()
else:
print("\033[0;37;41m INVALID INPUT !\033[0;37;48m\n")
def start_menu():
"""
Menu that allows players to select from instructions
scoretable or play the game
"""
global PLYR_SCORE
PLYR_SCORE = 0
print("\033[1;36;40m SELECT FROM THE MENU BELOW TO START:\033[0;37;48m")
print(" INSTRUCTIONS - A")
print(" SCORETABLE - B")
print(" PLAY GAME - C")
men_choice = input(" ENTER A, B OR C\n ")
print("______________________________________________________________\n")
if men_choice.upper() == "A":
instructions()
elif men_choice.upper() == "B":
scoretable()
elif men_choice.upper() == "C":
quest_catg()
else:
start_menu()
def set_cat(choice):
"""
This function sets the chosen category for the player
this value is held globally to avoid to much passing around
"""
global CHOS_CAT
if choice == "1":
CHOS_CAT = str("1")
print(" Category is Python\n")
elif choice == "2":
CHOS_CAT = str("2")
print(" Category is JS\n")
else:
CHOS_CAT = str("3")
print(" Category is HTML\n")
def validate_choice(choice):
"""
This function validates the user input to prevent error
and allows the while loop in other function to continue on
return false
"""
if choice not in ("1", "2", "3"):
print("\033[0;37;41m Choose category by typing 1, 2 or 3\033[0;37;48m")
return False
else:
return True
def quest_catg():
"""
This function asks the player to select a quiz category then
passes that value to the validator
"""
validated = 0
while validated < 1:
print("\033[1;32;40m SELECT A QUIZ CATEGORY\033[0;37;48m\n")
print(" PYTHON = 1 | JS = 2 | HTML = 3\n")
choice = input(" ENTER CATEGORY HERE - 1, 2 or 3:\n ")
if validate_choice(choice):
print(" Thank you for validating\n")
validated += 1
set_cat(choice)
q_l = set_questions()
play_game(0, 1, q_l)
def set_questions():
"""
This function uses the global chosen cat variable
to grab the correct questions from our external spreadsheet
this is then compiled to a list of lists and returned for use
in other functions
"""
print("\033[1;31;40m PLEASE ANSWER BY TYPING A, B or C !\033[0;37;48m")
print(" So you if you think the answer is B you would type B\n")
get_quest = SHEET.worksheet(f'{CHOS_CAT}').get_all_values()
return get_quest
def validate_question(answer):
"""
Checks for valid answer
This is explicitly set to only accept A, B or C
but does convert small to upper case to avoid
being overly picky
"""
if answer.upper() not in ("A", "B", "C"):
print("\033[0;37;41m ANSWER IS NOT VALID !\033[0;37;48m")
print(" Please answer with A, B or C !")
return False
else:
print(" \n")
return True
def check_question(answer, rng1, rng2, q_l):
"""
Checks to see if answer is correct or incorrect
converts the input again to uppercase to avoid confusion
then checks it against the answers pulled from out external sheet
ranges are used to control this
"""
global PLYR_SCORE
if answer.upper() == q_l[rng2][4]:
print("\033[1;32;40m CORRECT !\033[0;37;48m")
print("\033[1;32;40m SCORE + 1\033[0;37;48m\n")
PLYR_SCORE += 1
else:
print("\033[1;31;40m INCORRECT !\033[0;37;48m\n")
play_game(rng1, rng2 + 1, q_l)
def ask_question(rng1, rng2, q_l):
"""
Takes the range values from the the play game function and
uses the them to print the relevant questions from our
list of lists, checks valid input and then checks to
see if the answer is correct in a while loop
"""
quest_cnt = 1
while quest_cnt < 2:
print(f" QUESTION NUMBER: {rng2}")
for i in range(0, 4):
print(f"\033[1;34;40m {q_l[rng1][i]}:\033[0;37;48m {q_l[rng2][i]}")
answer = input("\033[1;32;40m Enter answer here:\033[0;37;48m\n ")
if validate_question(answer):
check_question(answer, rng1, rng2, q_l)
quest_cnt += 1
def game_over():
"""
accepts no values itself, runs on game over allows
user to input their name (which we strip of all whitespace)
and then stores this to our score spreadsheet, will not allow
inputs under 5 characters.
"""
print(f" Your score was {PLYR_SCORE}/10")
print(" GAME OVER!\n")
name_val = 1
while name_val < 2:
def remove(play_name):
return play_name.replace(" ", "")
play_name = input(" ENTER YOUR NAME TO SAVE YOUR SCORE\n ")
ply_nam = remove(play_name)
if ply_nam and 3 < len(ply_nam) < 9:
print(" \n")
str1 = ply_nam
str2 = PLYR_SCORE
final_str = f'{str1} ' + f'{str2}'
final_score = list(final_str.split(" "))
score_value = SHEET.worksheet("scores")
score_value.append_row(final_score)
name_val += 1
start_menu()
else:
print("\033[0;37;41m Enter a valid name...\033[0;37;48m")
print("\033[0;37;41m No blankspaces allowed...\033[0;37;48m")
print("\033[0;37;41m Between 4 & 8 characters long\033[0;37;48m\n")
def play_game(val1, val2, q_l):
"""
Passes the range values to our ask question function and also ensures
our list of lists (questions) are correctly passed around for use in
game loop. Each time this runs it increments a value which checks for
the end of the game (no more questions) based on a known value (10
currently but could be randomised or increased if required)
"""
print("\033[1;36;40m SCORE = " + f'{PLYR_SCORE}\033[0;37;48m')
if val2 < 11:
ask_question(val1, val2, q_l)
else:
game_over()
start_menu()