2014-08-31 130 views
0

我是新來學習python,我試圖編碼從英文到西班牙文的小詞翻譯。我在這裏得到的代碼從英文翻譯成西班牙文和西班牙文翻譯成英文。但是,我想添加一些代碼,允許用戶在輸入'show'時看到單詞列表。我得到了它的代碼,但是當我輸入show時,它只是打印出「除了keyerror」。Python製作詞典

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)) 

def translate(word): 
    try: 
     for key,value in english_to_spanish.items(): 
      if key == word: 
       print("{0} in Spanish is {1}".format(
              word, english_to_spanish[word])) 
      elif value == word: 
       print("{0} in English is {1}".format(
              word, key))   
    except KeyError: 
     print("That wasn't an option" 
      .format(translate)) 

print("Welcome to the English <--> Spanish Dictionary") 
while True: 
    word1 = input("> ") 
    translate(word1) 

這裏是我認爲會在用戶輸入'show'時向用戶顯示單詞列表的代碼。

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: 
     print("That wasnt an option") 

如果有人能夠幫助我在這裏,將不勝感激。 感謝

+3

當然, 「秀」引發了一個關鍵錯誤 - 它不在列表中。代碼發佈在底部的應該工作(*假設*'translate'沒有被調用,並且它被放在適當的位置)。顯示*實際代碼*很重要。 – user2864740 2014-08-31 08:34:35

+0

您的原始代碼使用'word1',而不是'word',用於用戶輸入。 – 2014-08-31 08:36:57

回答

0
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 ','.join(english_list) 
     elif wordlist == 'spanish': 
      print ','.join(spanish_list) 
    else: 
     try: 
      translate(word) 
     except Exception as e: 
      print str(e) 

這應該工作,但沒有測試。

我添加了spanish_english字典,因爲在您的解決方案中,您會爲字典中的每個查找做一個迭代。

+0

感謝您的回覆,但爲什麼我的代碼的最後一行出現無效的語法錯誤print str(e)? – 2014-08-31 08:59:59

+0

我有一個錯字if if =='show'(missing :)。 – gosom 2014-08-31 09:23:36

1

@gosom的答案是差不多了吧,除了一些小錯誤:

  1. 不幹「:」在行的結尾:「如果字==‘秀’」
  2. 如果您正在使用Python的2.x中,應更換 '輸入' 到 '的raw_input'

下面的代碼已經在Python的2.7.3進行了測試:

# -*- coding: utf-8 -*- 

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 = raw_input("> ") 
    if word == 'show': 
     wordlist = raw_input("Would you like to see the " 
          "English or Spanish wordlist?") 
     if wordlist == 'english': 
      print ','.join(english_list) 
     elif wordlist == 'spanish': 
      print ','.join(spanish_list) 
    else: 
     try: 
      translate(word) 
     except Exception as e: 
      print '--' 
      print str(e)