def displayScore(self, divisor, length): self.score /= divisor highestPossibleScore = str(length) if self.score < 10: print( inout.coloured( "Round over! Your score that round was " + '%.2g' % (self.score) + " out of " + highestPossibleScore + ", " + '%0.4g' % (self.score / int(highestPossibleScore) * 100) + "%.\n", 'cyan')) elif self.score < 100: print( inout.coloured( "Round over! Your score that round was " + '%.3g' % (self.score) + " out of " + highestPossibleScore + ", " + '%0.4g' % (self.score / int(highestPossibleScore) * 100) + "%.\n", 'cyan')) elif self.score < 1000: print( inout.coloured( "Round over! Your score that round was " + '%.4g' % (self.score) + " out of " + highestPossibleScore + ", " + '%0.4g' % (self.score / int(highestPossibleScore) * 100) + "%.\n", 'cyan')) self.score = 0
def load_words(): global wordSets try: # to load words with open(words_filepath, 'r') as wordsIn: sets_dict = json.load(wordsIn)["sets"] for ws in sets_dict: if not quiet: print(inout.coloured("Loading set of topic: " + ws['topic'], 'magenta')) word_list = [] if len(ws["words"]) > 0: if not quiet: print(inout.coloured("Loading words of topic: " + ws['topic'], 'magenta')) for w in ws["words"]: if w["type"] == "noun": word_list.append(add_noun(w["english"], w["language"], w['translation'], w["gender"], quiet=quiet)) elif w["type"] == "verb": word_list.append(add_verb(w["english"], w["language"], w['translation'], w["pastParticiple"], w["auxVerb"], quiet=quiet)) elif w["type"] == "adjective": word_list.append(add_adjective(w["english"], w["language"], w['translation'], quiet=quiet)) add_set(ws["language"], ws["topic"], word_list, True) if not quiet: print(inout.coloured(ws['topic'] + " successfully loaded.", 'magenta')) except FileNotFoundError: # Create file if not found if not quiet: print(inout.coloured("Failed to find file '" + words_filepath + "'.", 'yellow')) with open(words_filepath, 'w') as output: json.dump({}, output, -1, indent=2) except KeyError: # If there is nothing in words print(inout.coloured("You need to add some words.\nDon't forget to save!", 'yellow'))
def add_verb(english, language, translation, pastParticiple, auxVerb, topic=None, quiet=False): if topic is not None: wSet = findSet(topic, quiet, language) if wSet is not None: wSet.words.append(objects.Verb(english, language, translation, pastParticiple, auxVerb)) if not quiet: print(inout.coloured("Verb successfully added to set '" + topic + "'.", 'magenta')) else: if not quiet: print(inout.coloured("Verb successfully added.", 'magenta')) return objects.Verb(english, language, translation, pastParticiple, auxVerb)
def add_noun(english, language, translation, gender, topic=None, quiet=False): if topic is not None: wSet = findSet(topic, quiet, language) if wSet is not None: wSet.words.append(objects.Noun(english, language, translation, gender)) if not quiet: print(inout.coloured("Noun successfully added to set '" + topic + "'.", 'magenta')) else: if not quiet: print(inout.coloured("Noun successfully added.", 'magenta')) return objects.Noun(english, language, translation, gender)
def findWord(aSet, english, quiet): # Need to update found = None for i, w in enumerate(aSet.words): if w.english == english: found = i if not quiet: print(inout.coloured("Word '" + english + "' found in set '" + topic + "'.", 'magenta')) break else: if not quiet: print(inout.coloured("Word '" + english + "' not found in set '" + topic + "'.", 'magenta')) return None return found
def verbs( self, aSet, quizLength=0 ): # At the moment, this is for past prticiples. A conjugation quiz will be added soon quizLength = int(quizLength) verbs = [w for w in aSet.words if w.type == "verb"] deck = self.randomise(len(verbs)) numberOfQuestions = len(deck) while self.exit is False: try: index = deck.pop() questionNumber = numberOfQuestions - len(deck) except IndexError: self.displayScore(3, numberOfQuestions) self.exit = False return print( inout.coloured("Question " + str(questionNumber) + ":", 'cyan')) self.pastParticipleQuestion(verbs[index], 'a') self.auxiliaryVerbQuestion(verbs[index], 'b') self.translationQuestion(verbs[index], 'c') print() self.exit = False
def nouns(self, aSet, quizLength=0): quizLength = int(quizLength) nouns = [w for w in aSet.words if w.type == "noun"] deck = self.randomise(len(nouns)) numberOfQuestions = len(deck) while self.exit is False: try: index = deck.pop() questionNumber = numberOfQuestions - len(deck) except IndexError: self.displayScore(2, numberOfQuestions) self.exit = False return print( inout.coloured("Question " + str(questionNumber) + ":", 'cyan')) self.translationQuestion(nouns[index], 'a') self.genderQuestion(nouns[index], 'b') print() self.exit = False
def vocab(self, aSet, quizLength=0): # 0 is no length limit quizLength = int(quizLength) deck = self.randomise(len(aSet.words)) if quizLength > 0: deck = deck[:quizLength] numberOfQuestions = len(deck) while self.exit is False: try: index = deck.pop() questionNumber = numberOfQuestions - len(deck) except IndexError: self.displayScore(1, numberOfQuestions) self.exit = False return print( inout.coloured("Question " + str(questionNumber) + ":", 'cyan')) if aSet.words[index].type == "noun": self.translationQuestion(aSet.words[index], "a", 0.5) self.genderQuestion(aSet.words[index], "b", 0.5) else: self.translationQuestion(aSet.words[index]) print() self.exit = False
def findSet(topic, quiet, language=None, createSet=True): found = None for s in wordSets: # print(s.topic) if s.topic == topic: found = s if not quiet: print(inout.coloured("Set found for topic '" + topic + "'.", 'magenta')) break else: if not quiet: print(inout.coloured("Set for topic '" + topic + "' not found.", 'magenta')) if createSet and language is not None: add_set(language, topic, [], []) return wordSets[-1] return found
def pastParticipleQuestion(self, verb, letter=None, score=1): if self.exit is False: if letter is not None: self.displayQuestionLetter(letter) textIn = input( inout.coloured( "What is the past participle of '" + verb.translation + "'? ", 'cyan', True)).lower() if textIn == 'exit': self.exit = True elif textIn == verb.pastParticiple: print(inout.coloured("Correct!", 'green')) self.score += score else: print( inout.coloured( "Incorrect! The answer was " + verb.pastParticiple + ".", 'red')) if len(textIn) > 0: inout.remove_history_items(1)
def translationQuestion(self, word, letter=None, score=1): if self.exit is False: if letter is not None: self.displayQuestionLetter(letter) textIn = input( inout.coloured( "What is '{}' in English? ".format(word.translation), 'cyan', True)).lower() if textIn == "exit": self.exit = True elif textIn == word.english: print(inout.coloured("Correct!", 'green')) self.score += score else: print( inout.coloured( "Incorrect! The answer was " + word.english + ".", 'red')) if len(textIn) > 0: inout.remove_history_items(1)
def genderQuestion(self, noun, letter=None, score=1): if self.exit is False: if letter is not None: self.displayQuestionLetter(letter) textIn = input( inout.coloured( "What is the gender of '" + noun.translation + "'? ", 'cyan', True)).lower() if textIn == 'exit': self.exit = True elif textIn == noun.gender: print(inout.coloured("Correct!", 'green')) self.score += score else: print( inout.coloured( "Incorrect! The answer was " + noun.gender + ".", 'red')) if len(textIn) > 0: inout.remove_history_items(1)
def save_words(): global wordSets jsonFormat = {"sets": []} for ws in wordSets: word_list = [] for w in ws.words: word_list.append(w.__dict__) jsonFormat["sets"].append({"language": ws.language, "topic": ws.topic, "words": word_list}) with open(words_filepath, 'w') as output: json.dump(jsonFormat, fp=output, indent=2) print(inout.coloured("Words saved.", "magenta"))
def auxiliaryVerbQuestion(self, verb, letter=None, score=1): auxiliaryVerbs = ['haben', 'sein'] if verb.language == 'german' else [ 'avoir', 'être' ] # Shift-AltGr-6 release e for ê if self.exit is False: if letter is not None: self.displayQuestionLetter(letter) textIn = input( inout.coloured( "Does '" + verb.pastParticiple + "' use " + auxiliaryVerbs[0] + " or " + auxiliaryVerbs[1] + "? ", 'cyan', True)).lower() if textIn == 'exit': self.exit = True elif textIn == verb.auxVerb: print(inout.coloured("Correct!", 'green')) self.score += score else: print( inout.coloured( "Incorrect! The answer was " + verb.auxVerb + ".", 'red')) if len(textIn) > 0: inout.remove_history_items(1)
def ask(self, textIn): textIn = input(inout.coloured(textIn + ' ', self.colour, True)).lower() if self.record_history is False: self.eraseFromMemory() return textIn
def add_set(language, topic, words, quiet=False): global wordSets wordSets.append(objects.WordSet(language, topic, words)) if not quiet: print(inout.coloured("New topic "+topic+" created.", 'magenta'))
def displayQuestionLetter(self, letter): print(inout.coloured("{})".format(letter), "cyan"), end=' ')
print(" - French") print(" - German") print("Available topics:") print(" - Make your own! (see 'add word')") elif command == 'add word': print("add word") print("Usage: add word") print("Description: Begins asking the user a series of questions in order to add a word to the database.") load_words() quiz = objects.Quiz() running = True try: while running: action = input(inout.coloured("What would you like to do? ", 'blue', True)).lower() if len(action) > 0: if action == 'add word': english = input(inout.coloured("What is the word in English? ", 'magenta', True)).lower() language = input(inout.coloured("What language? ", 'magenta', True)).lower() translation = input(inout.coloured("What is the word in " + language + "? ", 'magenta', True)).lower() topic = input(inout.coloured("What topic is this word in? ", 'magenta', True)).lower() type = input(inout.coloured("What type of word is this? ", 'magenta', True)).lower() if type == 'noun': gender = input(inout.coloured("What is the gender of this noun? ", 'magenta', True)).lower() inout.remove_history_items(6) add_noun(english, language, translation, gender, topic) elif type == 'verb': pastParticiple = input(inout.coloured("What is the past participle of this verb? ", 'magenta', True)).lower()