Fixed a bug where the answer was deemed incorrect if it had uppercase letter in the .tst file

This commit is contained in:
Zdenek Borovec 2019-10-21 16:47:23 +02:00
commit e5467ba459
3 changed files with 40 additions and 29 deletions

Binary file not shown.

View file

@ -3,6 +3,8 @@ from tkinter import filedialog
from tkinter import messagebox
import random
import time
import multiprocessing
import sys
def swapSides(): # prohodi sloupce v souboru testu (a tim i zmeni co je otazka a co odpoved)
@ -21,17 +23,17 @@ def swapSides(): # prohodi sloupce v souboru testu (a tim i zmeni co je otazka a
def saveTest(): # ulozi soubor testu
print(test)
print(filepath)
with open(filepath, mode='w', encoding = "UTF-8") as file:
file.write(str(test[2])+'\n')
with open(filepath, mode='w', encoding="UTF-8") as file:
file.write(str(test[2]) + '\n')
for i in range(len(test[0])):
for j in range(len(test[0][i])):
file.write(test[0][i][j])
if j < len(test[0][i])-1:
if j < len(test[0][i]) - 1:
file.write(',')
file.write('/')
for j in range(len(test[1][i])):
file.write(test[1][i][j])
if j < len(test[1][i])-1:
if j < len(test[1][i]) - 1:
file.write(',')
file.write('\n')
@ -39,13 +41,13 @@ def saveTest(): # ulozi soubor testu
def newQuestion(): # nastavi novou otazku
global questionPlace
global timeAsked
questionPlace = random.randint(0, len(test[0])-1)
questionPlace = random.randint(0, len(test[0]) - 1)
newQuestion = test[0][questionPlace][0]
question.set(newQuestion)
timeAsked = time.time()-1
timeAsked = time.time() - 1
def on_closing(): #zobrazi se pri zavirani programu, pokud se uzivatel rozhodne program zavrit, tak prvne ulozi highscore
def on_closing(): # zobrazi se pri zavirani programu, pokud se uzivatel rozhodne program zavrit, tak prvne ulozi highscore
if messagebox.askokcancel("Quit", "Do you want to quit?"):
saveTest()
root.destroy()
@ -53,6 +55,7 @@ def on_closing(): #zobrazi se pri zavirani programu, pokud se uzivatel rozhodne
def submitAnswer(uselessArgument): # precte pole pro zadani odpovedi, zhodnoti ji, a vhodnym zpusobem zmeni score a highscore
global score
for i in test[1][questionPlace]:
if answerEntry.get().lower() == i.lower():
score += timeMultiplier
@ -63,6 +66,7 @@ def submitAnswer(uselessArgument): # precte pole pro zadani odpovedi, zhodnoti j
if score > test[2]:
test[2] = score
highScore.set('High score: %s' % (test[2]))
answerEntry.delete(0, 'end')
newQuestion()
currentScore.set('Score: %s' % round(score, 2))
@ -71,7 +75,7 @@ def submitAnswer(uselessArgument): # precte pole pro zadani odpovedi, zhodnoti j
def formatTest(filepath): # precte soubor a formatuje jej do listu test
global score
with open(filepath, encoding = "UTF-8") as file:
with open(filepath, encoding="UTF-8") as file:
file = file.readlines()
sloupec1 = []
@ -100,13 +104,15 @@ def openTest(): # ziska adresu testu a nastavi seznam test aby byl novy test, po
gotfile = False
filepath = None
while not gotfile:
filepath = filedialog.askopenfilename(title='Select test file')
filepath = filedialog.askopenfilename(title='Select test file', initialdir="///tests")
if getExtension(filepath) == '.tst':
gotfile = True
elif getExtension(filepath) == None:
pass
else:
messagebox.showinfo("ERROR: unrecognized file", "We are sorry but files with the '%s' appendix are not supported, we support only '.tst' files." % (getExtension(filepath)))
test = list(formatTest(filepath))
#newQuestion()
# newQuestion()
submitAnswer(0)
@ -125,14 +131,16 @@ def makeRoot(): # vytvori hlavni okno
root.geometry('500x400')
root.title('Test Me')
root.configure(bg="gray")
root.iconbitmap('programfiles/icon.ico')
root.iconbitmap('icon.ico')
return root
multiprocessing.freeze_support()
root = makeRoot()
filepath = 'programfiles/default.tst'
test = list(formatTest('programfiles/default.tst'))
filepath = 'default.tst'
test = list(formatTest('default.tst'))
questionPlace = 0
score = 0.0
@ -148,7 +156,7 @@ questionLabel = Label(root, textvariable=question, width=30)
spacingLabel = Label(bg='gray', padx=10)
answerEntry = Entry(root, width=30)
answerButton = Button(root, text='submit', command=lambda: submitAnswer(0))
timeMultiplierLabel = Label(root, text='Your time bonus is: %s' %timeMultiplier, bg='gray')
timeMultiplierLabel = Label(root, text='Your time bonus is: %s' % timeMultiplier, bg='gray')
anotherSpacingLabel.grid(row=0, column=0)
currentScoreLabel.grid(row=0, column=1, columnspan=2, sticky=W)
@ -159,8 +167,8 @@ answerEntry.grid(row=1, column=4, sticky=E)
answerButton.grid(row=1, column=5, sticky=W)
timeMultiplierLabel.grid(row=2, column=1, columnspan=5)
currentScore.set('Score: %s' %(score))
highScore.set('High score: %s' %(test[2]))
currentScore.set('Score: %s' % (score))
highScore.set('High score: %s' % (test[2]))
question.set('question will appear here')
root.protocol("WM_DELETE_WINDOW", on_closing)
@ -170,13 +178,16 @@ menu = makeMenu(root)
running = True
while(running):
while (running):
try:
if time.time() - timeAsked >=100:
if time.time() - timeAsked >= 100:
timeMultiplier = 1
else:
timeMultiplier = (1/(time.time()-timeAsked)*100)
timeMultiplierLabel.configure(text='Your time bonus is: %s' %round(timeMultiplier, 2))
timeMultiplier = (1 / (time.time() - timeAsked) * 100)
timeMultiplierLabel.configure(text='Your time bonus is: %s' % round(timeMultiplier, 2))
except:
None
try:
root.update()
except:
pass

BIN
vyzkousejme.exe.lnk Normal file

Binary file not shown.