2014-08-31 101 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)