用Python创建字典

0 投票
2 回答
1646 浏览
提问于 2025-04-19 01:31

我刚开始学习Python,想写一个小程序,把英语翻译成西班牙语。我找到了一个可以实现英语和西班牙语互译的代码。不过,我想加一个功能,让用户输入“show”时能看到单词列表。我有这个功能的代码,但当我输入“show”时,它只显示“except keyerror”的错误信息。

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

如果有人能帮我解决这个问题,我会非常感激。谢谢!

2 个回答

1

@gosom的回答基本正确,但有一些小错误:

  1. 在“if word == 'show'”这一行的末尾要去掉一个冒号':'
  2. 如果你使用的是Python 2.x版本,应该把'input'改成'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)
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)

这个应该可以用,但还没测试过。

我添加了西班牙语-英语的字典,因为在你的解决方案中,每次查找字典时都要进行一次循环。

撰写回答