外语测验和计数结果

2024-06-02 07:11:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要写一个程序,测试用户对外来词的翻译。基本上,程序提供了一个单词,用户需要输入英语单词和西班牙语单词。每次有正确答案时,程序都给出肯定的回答;对于每一个错误,请给出正确答案。程序还需要记录分数,并报告最后有多少答案是正确的

from random import shuffle

english_list = ["fire","apple","morning","river","wind","computer science",
                "biology","airplane","motorcycle","house","shower","wall",
                "eye","finger","head","goat","bird","dog","cat","office",
                "city"]

spanish_list = ["fuego","manzana","mañana","río","viento","informática",
                "biología","avión","motocicleta","casa","ducha","pared",
                "ojo","dedo","cabeza","cabra","ave","perro","gato","officina",
                "ciudad"]

def quizTime():
    english_to_spanish = dict(zip(english_list, spanish_list))
    spanish_to_english = dict(zip(spanish_list, english_list))

    print("Welcome to the English to Spanish Quiz! You will be tested"
          " on your knowledge of vocabulary words")

    print("Please type in the correct translation")

    for spanishWord, englishWord in spanish_to_english.items():
        response = input('What is the English word for ' + spanishWord + '? ')
        print(response == englishWord)

这段代码应该是在整个西班牙语词典上测试用户。我不知道如何将代码设置为测验格式,以便用户能够有效地与之交互。我应该用pythons字典里的外来词。如何只使用Python字典来访问这些外来词来编写类似的代码。本质上,我不能像在这段代码中那样使用zip函数


Tags: theto答案代码用户程序englishzip
1条回答
网友
1楼 · 发布于 2024-06-02 07:11:16

我将只尝试修复你已经在问题中的代码,因为这是一个家庭作业,我猜,你可以扩展这个

首先,您没有使用导入的random.shuffle
另外,您不需要创建两个zip,因为您已经知道所有西班牙语到英语的翻译都在同一索引中,所以您只需要同时遍历两个列表,在同一索引中获取单词,然后查看猜测是否与英语单词匹配

english_list = ["fire","apple","morning","river","wind","computer science",
                "biology","airplane","motorcycle","house","shower","wall",
                "eye","finger","head","goat","bird","dog","cat","office",
                "city"]

spanish_list = ["fuego","manzana","mañana","río","viento","informática",
                "biología","avión","motocicleta","casa","ducha","pared",
                "ojo","dedo","cabeza","cabra","ave","perro","gato","officina",
                "ciudad"]

def quizTime():

    print("Welcome to the English to Spanish Quiz! You will be tested"
          " on your knowledge of vocabulary words")

    print("Please type in the correct translation")

    #Iterate through both lists
    for idx in range(len(spanish_list)):
        #Get the spanish and english word at same index
        spanishWord = spanish_list[idx]
        englishWord = english_list[idx]

        #Ask for user input and check if the translation matches
        response = input('What is the English word for ' + spanishWord + '? ')
        print(response == englishWord)

quizTime()

输出将是

Welcome to the English to Spanish Quiz! You will be tested on your knowledge of vocabulary words
Please type in the correct translation
What is the English word for fuego? fire
True
What is the English word for manzana? apple
True
What is the English word for mañana? morning
True
What is the English word for río? 
.....

更新:

如果您的输入是一个字典,那么它将是一个西班牙语到英语的字典,而不是像注释中那样的英语到西班牙语的字典,因为我们要查找value英语单词和key西班牙语单词,因此它看起来是这样的:

vocabList = {'fuego': 'fire', 'manzana': 'apple', 'manana': 'morning', 'rio': 'river', 'viento': 'wind', 'informatica': 'computer science',
             'biologia': 'biology', 'avion': 'airplane', 'motocicleta': 'motorcycle', 'casa': 'house', 'ducha': 'shower', 'pared': 'wall',
             'ojo': 'eye', 'dedo': 'finger', 'cabenza': 'head', 'cabra': 'goat', 'ave': 'bird', 'perro': 'dog', 'gato': 'cat', 'officina': 'office', 'ciudad': 'city'}

def quizTime():

    print("Welcome to the English to Spanish Quiz! You will be tested"
          " on your knowledge of vocabulary words")

    print("Please type in the correct translation")

    #Iterate through key and value of dictionary
    for spanishWord, englishWord in vocabList.items():

        #Ask for user input and check if the translation matches
        response = input('What is the English word for ' + spanishWord + '? ')
        print(response == englishWord)

quizTime()

相关问题 更多 >