Python 创建一个测试用户外语词汇的程序

Python 创建一个测试用户外语词汇的程序,python,python-3.x,Python,Python 3.x,我需要写一个程序,测试用户对外来词的翻译。基本上,程序提供了一个单词,用户需要输入所提供的西班牙语单词的英语单词。每次有正确答案时,程序都会给出肯定的回答;对于每个错误,请给出正确答案。程序还需要记录分数,并报告最后有多少答案是正确的 english_list = ["fire","apple","morning","river","wind"] spanish_list = ["fuego","manzana","mañana","río","viento"] english_to_spani

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

english_list = ["fire","apple","morning","river","wind"]
spanish_list = ["fuego","manzana","mañana","río","viento"]
english_to_spanish = dict(zip(english_list, spanish_list))
spanish_to_english = dict(zip(spanish_list, english_list))

def translate(word):
    translation = english_to_spanish.get(word)
    if translation:
        return translation

    translation = spanish_to_english.get(word)
    if translation:
        return translation

    raise Exception('Word {0} does not exists'.format(word))

print("Welcome to the English <--> Spanish Quiz")
english\u list=[“火”、“苹果”、“早晨”、“河”、“风”]
西班牙语列表=[“火地岛”、“曼扎纳”、“玛尼亚那”、“里奥”、“维也纳”]
英语到西班牙语=dict(zip(英语列表,西班牙语列表))
西班牙语到英语=dict(zip(西班牙语列表,英语列表))
def翻译(word):
翻译=英语到西班牙语。获取(word)
如果翻译:
返回翻译
翻译=西班牙语到英语。获取(word)
如果翻译:
返回翻译
引发异常('字{0}不存在'。格式(字))
打印(“欢迎参加英语-西班牙语测验”)
这段代码应该在整个西班牙语词典上测试用户。我不知道如何以问答形式设置代码,以便用户能够有效地与之交互。我应该使用pythons字典中的外来词。如果只使用Python字典来访问这些外来词,如何编写类似的代码呢

print('Type each translation, or just hit ENTER to exit.')

for sp, en in spanish_to_english.items():
    response = input(sp + '? ')
    if response == '':
        break  # empty response -> end of quiz
    print(response == en)
    print('')
将显示
响应==en
布尔表达式 作为
True
False

如果需要,可以选择显示正确答案:

    print(response == en)
    if response != en:
        print('The correct answer is:', en)
将显示
响应==en
布尔表达式 作为
True
False

如果需要,可以选择显示正确答案:

    print(response == en)
    if response != en:
        print('The correct answer is:', en)

我建议
while
使用
中的第一行循环,而
循环是
answer=input('you like next word?(Y/N)
并处理用户输入我建议
使用
中的第一行循环,而
循环是
answer=input('you like next word?(Y/N)'
和处理用户输入有没有办法向代码中添加一个部分,如果您键入的单词不正确,该部分将返回错误消息?有没有办法向代码中添加一个部分,如果您键入的单词不正确,该部分将返回错误消息?