我需要编写一个程序来询问用户外语翻译。基本上,该程序提供一个单词,用户将键入所提供的西班牙语单词的英文单词。每当有一个正确的答案时,程序都会给出肯定的回答。对于每个错误,给出正确答案。该程序还需要保持分数并在最后报告正确答案的数量。
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函数。
答案 0 :(得分:0)
我将仅尝试解决问题中已经存在的代码,由于我认为这是一项家庭作业,因此您可以对此进行扩展
首先,您没有使用导入的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()