-
Notifications
You must be signed in to change notification settings - Fork 0
/
studious.py
148 lines (127 loc) · 5.07 KB
/
studious.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
import random
import time
import json
import base64
# Todo Shuffle Answers
KEY = 'LS0gKGMpIDIwMjMgQWxleCBWZXJnYXJhIC0t'
FILENAME = "QA.json"
def Main():
print(base64.b64decode(KEY))
MODE = input("Select a mode: (ADD) (STUDY) \n")
A = "ADD"; B = "STUDY"
if MODE.lower() == A.lower():
print("~ ADDING MODE ~ \n")
AddMode()
elif MODE.lower() == B.lower():
print("~ STUDY MODE ~ \n")
StudyMode()
def AddMode():
QUESTION = input("Input Question: ")
NUM_ANSWERS = input("Input # of Answers (max=4): ")
if (NUM_ANSWERS.isdigit() and int(NUM_ANSWERS) <= 4):
TEMP_ARRAY = []
for i in range(int(NUM_ANSWERS)):
ANSWER = input("Input an Answer: ")
TEMP_ARRAY.append(ANSWER)
# Evaluate which answer is the correct one & store that
ITTR_VALUE = 0
for text in TEMP_ARRAY:
ITTR_VALUE+=1
print("(" + str(ITTR_VALUE) + ") '" + text + "'")
CORRECT_ANSWER = input("Indicate which answer is correct by typing it's corresponding number: ")
if (CORRECT_ANSWER.isdigit() and int(CORRECT_ANSWER) <= int(NUM_ANSWERS)):
print("The correct answer is: '" + TEMP_ARRAY[int(CORRECT_ANSWER)-1] + "'\n")
#ANSWER_TITLE = TEMP_ARRAY[int(CORRECT_ANSWER)-1]
RESULT = {"question":str(QUESTION),
"answers":TEMP_ARRAY,
"correct_answer":CORRECT_ANSWER#ANSWER_TITLE
}
Write_JSON(RESULT)
# Ask if the user would like to add more
CONTINUE = input("Would you like to continue? (y/n/s) \n")
if CONTINUE.lower() == "y":
AddMode()
elif CONTINUE.lower() == "n":
exit()
elif CONTINUE.lower() == "s":
StudyMode()
else:
print("Invalid Input")
exit()
PREVIOUS_QUESTIONS = []
COUNT = 0
RIGHT_CNT = 0
WRONG_CNT = 0
TRACK_QUESTIONS = 0
def EvalChoices():
global RIGHT_CNT
global WRONG_CNT
TOTAL = RIGHT_CNT+WRONG_CNT
if TOTAL != 0:
RATIO = RIGHT_CNT/TOTAL
print("You Finished! - Right: " + str(RIGHT_CNT) + ", Wrong: " + str(WRONG_CNT) + ", Total Questions: " + str(TOTAL))
print("Final Score: " + str(round(RATIO, 2)*100) + "%")
elif TOTAL == 0:
print("No questions were answered.")
def StudyMode():
global COUNT
global TRACK_QUESTIONS
global FILENAME
with open(FILENAME, "r") as f:
QA = json.load(f)
for x in QA['problems']:
# Pick a random problem
RAND = random.choice(list(QA['problems']))
#print(PREVIOUS_QUESTIONS)
if str(RAND).lower() not in PREVIOUS_QUESTIONS:
print("PROBLEMS LEFT = " + str(len(QA['problems'])-TRACK_QUESTIONS) + "\n")
#print("CURRENT_QUESTION= " + str(RAND) + " PREVIOUS_QUESTION= " + PREVIOUS_QUESTIONS + "\n")
print("Question: " + RAND['question'])
ITTR_VALUE = 0
# Display all answers
for text in RAND['answers']:
ITTR_VALUE+=1
print("(" + str(ITTR_VALUE) + ") '" + text + "'")
def CheckInput():
global RIGHT_CNT
global WRONG_CNT
global TRACK_QUESTIONS
RESULT = input("Answer (q=quit): ")
# Check if RESULT is the correct answer
if (RESULT.isdigit() and int(RESULT) <= ITTR_VALUE and int(RESULT) > 0):
#print("RESULT = " + RESULT)
#print("CORRECT_ANSWER = " + str(RAND['correct_answer']))
if (int(RESULT) == int(RAND['correct_answer'])):
RIGHT_CNT+=1
print("You got it RIGHT! \n")
elif (int(RESULT) != int(RAND['correct_answer'])):
WRONG_CNT+=1
print("You got it WRONG. The answer was: '" + str(RAND['correct_answer']) + "'\n")
TRACK_QUESTIONS+=1 #ADD TO TRACK QUESTIONS
elif (RESULT.lower() == "q"):
EvalChoices()
exit()
else:
print("Invalid Input. \n")
CheckInput()
CheckInput()
elif str(RAND).lower() in PREVIOUS_QUESTIONS:
#print("SAME QUESTION")
#print(len(QA['problems']))
if COUNT >= len(QA['problems'])*5: #THIS MULTIPLE IS IMPORTANT
EvalChoices()
exit()
COUNT+=1
StudyMode()
PREVIOUS_QUESTIONS.append(str(RAND).lower())
def Write_JSON(new_data, filename=FILENAME):
with open(filename,'r+') as file:
# First we load existing data into a dict.s
file_data = json.load(file)
# Join new_data with file_data
file_data["problems"].append(new_data)
# Sets file's current position at offset.
file.seek(0)
# convert back to json.
json.dump(file_data, file, indent = 4)
Main()