Māori_to_Eng_Quiz

Create an English to Māori translator to get Māori translations for random English words from english_words package. We will then pick 10 random words from the translated Māori words list. Following that, we will conduct a quiz, where users are asked to translate Māori words into English
 avatar
user_0566003
python
a year ago
1.7 kB
5
Indexable
Never
from translate import Translator
import random
from english_words import english_words_lower_alpha_set

def create_translator(to_lang):
    return Translator(to_lang=to_lang)

def translate_english_words_to_maori(english_words, translator):
    translated_words = {}
    for word in english_words:
        translation = translator.translate(word)
        translated_words[translation] = word
    return translated_words

def conduct_quiz(quiz_words, translated_words):
    score = 0
    for i, word in enumerate(quiz_words):
        # Ask the question
        user_answer = input(f"Q{i+1}. Please translate '{word}' into English: ").lower()
        correct_answer = translated_words[word]

        # Check the user's answer
        if user_answer == correct_answer:
            print("Correct! Good job!")
            score += 1
        else:
            print(f"Incorrect! The correct answer is '{correct_answer}'")
        print('*****************************************\n'
              '*****************************************\n')
    return score

def main():
    # Define the translators
    translator_to_maori = create_translator("mi")

    # Get a set of 100 random English words
    random_english_words = random.sample(english_words_lower_alpha_set, 100)

    # Translate the English words to Maori
    translated_words = translate_english_words_to_maori(random_english_words, translator_to_maori)

    # Pick 10 random words from the translated Maori words for the quiz
    quiz_words = random.sample(list(translated_words.keys()), 10)

    # Conduct the quiz
    score = conduct_quiz(quiz_words, translated_words)

    print(f'Good Job! Your final score is {score}')

if __name__ == "__main__":
    main()