我是新來學習python,我正在研究一個迷你翻譯器,將英語譯成西班牙語和西班牙語譯成英語。用戶可以選擇輸入'show'來查看英文或西班牙文列表。Python-列表到詞典
有人可以幫助我理解,爲什麼當他們鍵入列表中的英語或西班牙語單詞時,它不會翻譯該單詞。當我說它沒有翻譯,沒有任何反應,我可以繼續輸入新單詞,它什麼都不做。 'show'部分雖然正常工作。
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 Dictionary")
while True:
word = input("> ")
if word == 'show':
wordlist = input("Would you like to see the English or Spanish wordlist?")
if wordlist == 'english':
print(english_list)
elif wordlist == 'spanish':
print(spanish_list)
else:
try:
translate(word)
except Exception as e:
print ("That wasn't a option")
任何幫助,將不勝感激。謝謝
你會得到什麼錯誤?你使用的是什麼版本的Python? – inspectorG4dget 2014-09-01 05:47:45
我正在使用3.41,當我輸入文字時,沒有任何反應,除非輸入'show',否則我可以繼續輸入任何內容。 – 2014-09-01 05:49:52