首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >函数的返回值被忽略

函数的返回值被忽略
EN

Stack Overflow用户
提问于 2017-10-06 10:55:52
回答 1查看 23关注 0票数 0
代码语言:javascript
复制
VOWELS = ['a', 'e', 'i', 'o', 'u']

BEGINNING = ["th", "st", "qu", "pl", "tr"]


def pig_latin2(word):
    # word is a string to convert to pig-latin
    string = word
    string = string.lower()
    # get first letter in string
    test = string[0]
    if test not in VOWELS:
        # remove first letter from string skip index 0
        string = string[1:] + string[0]
        # add characters to string
        string = string + "ay"
    if test in VOWELS:
        string = string + "hay"
    print(string)


def pig_latin(word):
    string = word
    transfer_word = word
    string.lower()
    test = string[0] + string[1]
    if test not in BEGINNING:
        pig_latin2(transfer_word)

    if test in BEGINNING:
        string = string[2:] + string[0] + string[1] + "ay"
    print(string)

当我在上面两个函数中取消注释下面的代码并用返回字符串替换print( string )时,它只适用于pig_latin()中的单词。一旦word被传递给pig_latin2(),我就会得到所有单词和程序崩溃的值为None。

代码语言:javascript
复制
# def start_program():
    # print("Would you like to convert words or sentence into pig latin?")
    # answer = input("(y/n) >>>")
    # print("Only have words with spaces, no punctuation marks!")
    # word_list = ""
    # if answer == "y":
    #   words = input("Provide words or sentence here: \n>>>")
    #   new_words = words.split()
    #   for word in new_words:
    #       word = pig_latin(word)
    #       word_list = word_list + " " + word
    #   print(word_list)

    # elif answer == "n":
    #   print("Goodbye")
    #   quit()


    # start_program()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-06 11:32:21

您没有捕获pig_latin2函数的返回值。所以不管这个函数做什么,你都要放弃它的输出。

修正pig_latin函数中的这一行:

代码语言:javascript
复制
if test not in BEGINNING:
    string = pig_latin2(transfer_word)  # <----------- forgot 'string =' here

当它固定的时候,它对我起作用。话虽如此,还是有一堆东西要清理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46604170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档