我需要写一个程序,测试用户对外来词的翻译。基本上,程序提供了一个单词,用户需要输入英语单词和西班牙语单词。每次有正确答案时,程序都给出肯定的回答;对于每一个错误,请给出正确答案。程序还需要记录分数,并报告最后有多少答案是正确的
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函数
我将只尝试修复你已经在问题中的代码,因为这是一个家庭作业,我猜,你可以扩展这个
首先,您没有使用导入的
random.shuffle
另外,您不需要创建两个zip,因为您已经知道所有西班牙语到英语的翻译都在同一索引中,所以您只需要同时遍历两个列表,在同一索引中获取单词,然后查看猜测是否与英语单词匹配
输出将是
更新:
如果您的输入是一个字典,那么它将是一个西班牙语到英语的字典,而不是像注释中那样的英语到西班牙语的字典,因为我们要查找
value
英语单词和key
西班牙语单词,因此它看起来是这样的:相关问题 更多 >
编程相关推荐