-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriviagame (with quiz excel file).py
57 lines (43 loc) · 1.21 KB
/
triviagame (with quiz excel file).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
import random
#index position:
#0: question
#1-4: 4 possible answers
# 5: Category
# 6: difficulty
def ask_question(q, p):
print()
print(q[0])
for i in range(1, 5):
print(i, q[ p[i-1] ])
def raw_read_answer():
try:
ans = int(input("Answer (1, 2, 3, or 4): "))
return ans
except ValueError:
print("Error. Answer must be an integer.")
def read_answer():
ans = raw_read_answer()
while ans not in [1,2,3,4]:
print("Answer must be 1, 2, 3, or 4")
ans = raw_read_answer()
return ans
# Tesitng while coding --> main
lines = open('quiz.csv').read().splitlines() # or .split('\n')
questions = []
for line in lines:
questions.append(line.split('\t'))
lives = 3
score = 0
while lives >= 1:
p = [1,2,3,4]
random.shuffle(p)
q = random.choice(questions)
ask_questions(q, p)
ans = read_answer()
if p[ans - 1] == 1:
print("Correct")
score = score + int(q[-1])
else:
print("Incorrect. The correct answer is: ", q[1])
lives = lives - 1
print("You have", lives, "lives left. Your score is: ", score)