Python制作词典

Python制作词典,python,Python,我刚开始学习python,我正在尝试编写一个从英语到西班牙语的小词翻译程序。我在这里得到了从英语到西班牙语和西班牙语到英语的翻译代码。但是,我想添加代码,允许用户在输入“show”时查看单词列表。我得到了它的代码,但当我输入show时,它只是打印出“except keyrerror” 如果有人能在这里帮助我,我将不胜感激。 谢谢英语列表=[“火”、“苹果”、“早晨”、“河”、“风”] 西班牙语列表=[“火地岛”、“曼扎纳”、“玛尼亚那”、“里奥”、“维也纳”] 英语到西班牙语=dict(zip(

我刚开始学习python,我正在尝试编写一个从英语到西班牙语的小词翻译程序。我在这里得到了从英语到西班牙语和西班牙语到英语的翻译代码。但是,我想添加代码,允许用户在输入“show”时查看单词列表。我得到了它的代码,但当我输入show时,它只是打印出“except keyrerror”

如果有人能在这里帮助我,我将不胜感激。 谢谢

英语列表=[“火”、“苹果”、“早晨”、“河”、“风”]
西班牙语列表=[“火地岛”、“曼扎纳”、“玛尼亚那”、“里奥”、“维也纳”]
英语到西班牙语=dict(zip(英语列表,西班牙语列表))
西班牙语到英语=dict(zip(西班牙语列表,英语列表))
def翻译(word):
翻译=英语到西班牙语。获取(word)
如果翻译:
返回翻译
翻译=西班牙语到英语。获取(word)
如果翻译:
返回翻译
引发异常('字{0}不存在'。格式(字))
打印(“欢迎使用英语-西班牙语词典”)
尽管如此:
word=输入(“>”)
如果word=='show':
wordlist=input(“您想查看英语或西班牙语单词列表吗?”)
如果单词列表==“英语”:
打印“,”。加入(英文列表)
elif wordlist==“西班牙语”:
打印“,”。加入(西班牙语列表)
其他:
尝试:
翻译(word)
例外情况除外,如e:
打印str(e)
这应该是可行的,但没有经过测试


我添加了西班牙语和英语dict,因为在您的解决方案中,您对字典中的每个查找都进行了迭代。

除了一些小错误外,@gosom的答案几乎是正确的:

  • 退出“if word=='show'”行末尾的“:”命令
  • 如果您使用的是Python2.x,那么应该将“input”替换为“raw\u 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)
    
    #-*-编码:utf-8-*-
    英语列表=[“火”、“苹果”、“早晨”、“河”、“风”]
    西班牙语列表=[“火地岛”、“曼扎纳”、“玛尼亚那”、“里奥”、“维也纳”]
    英语到西班牙语=dict(zip(英语列表,西班牙语列表))
    西班牙语到英语=dict(zip(西班牙语列表,英语列表))
    def翻译(word):
    翻译=英语到西班牙语。获取(word)
    如果翻译:
    返回翻译
    翻译=西班牙语到英语。获取(word)
    如果翻译:
    返回翻译
    引发异常('字{0}不存在'。格式(字))
    打印(“欢迎使用英语-西班牙语词典”)
    尽管如此:
    word=原始输入(“>”)
    如果word=='show':
    wordlist=raw\u输入(“您想查看
    “英语或西班牙语词表?”)
    如果单词列表==“英语”:
    打印“,”。加入(英文列表)
    elif wordlist==“西班牙语”:
    打印“,”。加入(西班牙语列表)
    其他:
    尝试:
    翻译(word)
    例外情况除外,如e:
    打印'--'
    打印str(e)
    
    当然,“show”会引发一个关键错误-它不在列表中。底部的代码应该可以工作(假设未调用
    translate
    ,并将其放置在适当的位置)。显示实际代码很重要。您的原始代码使用
    word1
    ,而不是
    word
    ,用于用户输入。感谢您的回复,但为什么在您的代码打印str(e)的最后一行上出现无效语法错误?
    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")
    
    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)
    
    # -*- 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)