-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
131 lines (118 loc) · 4.48 KB
/
Copy pathmain.py
File metadata and controls
131 lines (118 loc) · 4.48 KB
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
import json, random
"""Necessary Files"""
qs_file=open("questions.json","r",encoding="utf-8")
qs=json.load(qs_file)
h_file=open("history.json","r",encoding="utf-8")
h=json.load(h_file)
lb_file=open("leaderboard.json","r",encoding="utf-8")
lb=json.load(lb_file)
"""Global Variables"""
correct=0
false=0
percentage=0
name="Player"
"""Function For Changing Name"""
def change_name():
global name
name=input(f"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nCurrent name: {name}\n\n\n\n\nNew name: ")
main()
"""Function For Displaying Stats"""
def stats():
global percentage
percentage=correct/10*100
input(f"\n\n\n\n\n\n\n\nPlayer {name}\n\n\n\nCorrects: {correct}\n\n\nFalses: {false}\n\n\nSuccess Percentage: {percentage}%\n\n\n\n\nPress any key to continue....")
h.append({"name":name,"corrects":correct,"falses":false,"success_percentage":percentage})
with open("history.json","w",encoding="utf-8") as h_file:
json.dump(h, h_file)
main()
"""Function For Displaying Leaderboard"""
def leaderboard():
new=sorted(h,key=lambda x:x["success_percentage"], reverse=True)
with open("leaderboard.json","w",encoding="utf-8") as lb_file:
json.dump(new,lb_file)
try:
print("\n\n\n\n\n\n\n\n\n\n\n\n")
place=1
for p in lb:
print(f"\n====================================== {place} ======================================\n")
print(f"Name: {p["name"]}\nSucces Percentage: {p["success_percentage"]}")
place+=1
input("\n\n\n\n\n\n\n\nPress any key to continue")
except SyntaxError:
input("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nFile is unaviable...\n\n\nPress enter for backing the menu\n\n\n\n\n\n\n\n\n")
main()
"""Function For Displaying Game Hstory"""
def history():
try:
print("\n\n\n\n\n\n\n\n\n\n\n\n")
game=1
for g in h:
print(f"\n======================================Game {game}======================================\n")
print(f"Name: {g["name"]}\nCorrects: {g["corrects"]}\nFalses: {g["falses"]}\nSucces Percentage: {g["success_percentage"]}")
game+=1
input("\n\n\n\n\n\n\n\nPress any key to continue")
except SyntaxError:
input("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nFile is unaviable...\n\n\nPress enter for backing the menu\n\n\n\n\n\n\n\n\n")
main()
"""Function For Displaying Question"""
def display_question(v_a,v_b,v_c,v_d,q,ans):
global correct,false
print(f"\n\n\n\n\n\n\n\n\n\n\n\n\n======================================Question {len(qs_id)}======================================\n")
print(q)
print("\n\n\n\n")
slc_ans=input(f"1.{v_a} 2.{v_b}\n\n3.{v_c} 4.{v_d}\n\nSelect an answer: ")
if (slc_ans=="1" and v_a==ans)or(slc_ans=="2" and v_b==ans)or(slc_ans=="3" and v_c==ans)or(slc_ans=="4" and v_d==ans):
correct+=1
input("\n\n\n\n\n\n\n\n\n\n\nCorrect\n\n\n\n\n\nPress any key to continue...\n\n\n\n\n\n")
quizz()
else:
false+=1
input("\n\n\n\n\n\n\n\n\n\n\nFalse!\n\n\n\n\n\nPress any key to continue...\n\n\n\n\n\n")
quizz()
"""Function For Generating Question"""
def quizz():
global qs_id
if len(qs_id)==10:
stats()
return
q_id=random.randint(0,19)
if q_id in qs_id:quizz()
else:
qs_id.append(q_id)
q=qs[q_id]["question"]
options=qs[q_id]["options"]
ans=qs[q_id]["correct_answer"]
va_id=random.randint(0,3)
v_a=options[va_id]
options.pop(va_id)
vb_id=random.randint(0,2)
v_b=options[vb_id]
options.pop(vb_id)
vc_id=random.randint(0,1)
v_c=options[vc_id]
options.pop(vc_id)
v_d=options[0]
display_question(v_a,v_b,v_c,v_d,q,ans)
"""Function For Genereting Quizz"""
def start_quizz():
global qs_id
qs_id=[]
quizz()
"""Function For Displaying Menu"""
def options():
opt=input("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n1.Start Quizz\n2.Leaderboard\n3.History\n4.Change Name\n0.Exit\n\nSelect an option: ")
if opt=="1":start_quizz()
elif opt=="2":leaderboard()
elif opt=="3":history()
elif opt=="4":change_name()
elif opt=="0":
print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nExiting...")
exit(0)
else:
print("Write a valid option!")
main()
"""Main Function"""
def main():
options()
"""Running Code"""
main()