简体   繁体   English

如何引用函数的返回值?

[英]How to reference the return value from a function?

My assignment is to use nested if statements to construct a simple translator. 我的任务是使用嵌套的if语句构造一个简单的转换器。 Yes, I know that's an awful way to do it, but that's what the assignment says. 是的,我知道这样做很糟糕,但这就是作业要说的。 Here's what I have so far: 这是我到目前为止的内容:

def translate(word, language):
    '''
    Takes as parameters a string to translate,
    and a second string as the language to translate to.
    Returns the original word, the language, and the word's
    translation in the entered language.
    '''
    en_word = word.lower()
    lang = language.lower()

    #   French
    if lang == "french":
        if en_word == "cat":
            print("chat")            
    if lang == "french":
        if en_word == "dog":
            print("chein")

    #   German      
    if lang == "german":
        if en_word == "cat":
            print("katze")
    if lang == "german":
        if en_word == "dog":
            print("hund")

    #   Spanish      
    if lang == "spanish":
        if en_word == "cat":
            print("gato")
    if lang == "spanish":
        if en_word == "dog":
            print("perro")

translate('cat', 'french')

After being called as translate('cat', 'french') , for example, the output should be: 例如,在被称为translate('cat', 'french') ,输出应为:
The translation of cat in French is chat

As it is, my function finds and prints the correct word. 实际上,我的函数找到并打印了正确的单词。 I tried using a return statement such as 我尝试使用return语句,例如

return en_word, 'French', 'chat' and a print statement such as return en_word, 'French', 'chat'和打印语句,例如

print('The translation of', translate[0], 'into', translate[1], 'is', translate[2])

but the error message TypeError: 'function' object is not subscriptable popped up. 但是错误消息TypeError: 'function' object is not subscriptable弹出。 I could put virtually identical print statements into each end of the if statements in the function, but that seems terribly inelegant. 我可以在函数的if语句的每一端放入几乎相同的print语句,但这看起来非常不雅致。

How can I get the function to return the English word, the language and the translated word to insert into a generic print statement? 如何获得返回英文单词,语言和翻译后的单词以插入通用打印语句的功能?

用return语句替换print函数?

Here is an example of how you can restructure your code. 这是如何重组代码的示例。

def translate(word, language):

    en_word = word.lower()
    lang = language.lower()

    if lang == "french":
        if en_word == "cat":
            return "chat"
        elif en_word == "dog":
            return "chein"

    elif lang == "german":
        if en_word == "cat":
            return "katze"
        elif en_word == "dog":
            return "hund"

    elif lang == "spanish":
        if en_word == "cat":
            return "gato"
        elif en_word == "dog":
            return "perro"

myword = 'cat'
mylanguage = 'french'

res = translate(myword, mylanguage)
print('The translation of {0} in {1} is {2}'.format(myword, mylanguage.title(), res))

Explanation 说明

  • Use elif instead of unnecessary repeated if statements. 使用elif代替不必要的重复if语句。
  • Have your function return a value instead of print . 让函数return一个值而不是print
  • Use str.format to incorporate variables in your print statement outside your function. 使用str.format将变量str.format到函数外部的print语句中。

I guess this is specified by your assignment, but in real life, I would use a dictionary of dictionaries as the basis of your ... dictionary and not a bunch of if statements: 我猜这是由您的作业指定的,但是在现实生活中,我会使用字典的字典作为您...字典的基础,而不是一堆if语句:

def translate(word,language):
    dictionary = {
      "french": {"cat":"chat",
                 "dog":"chien"},
      "german": {"cat":"katze",
                 "dog":"hund"},
      "spanish": {"cat":"gato",
                  "dog":"perro"}
     }
return(dictionary.get(language).get(word,word))

This yields: 这样产生:

>>> translate("cat","french")
'chat'
>>> translate("cat","german")
'katze'
>>> translate("dog","german")
'hund'
>>> translate("mouse","spanish")
'mouse'

Here it returns the original word if it is not found. 如果找不到原始单词,它将返回原始单词。 You'd want to build in error handling for words or languages that weren't in dictionary, 您想对字典中没有的单词或语言进行错误处理,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM