函数的返回值被忽略

时间:2017-10-06 10:55:52

标签: python-3.x

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()中的单词。一旦将单词传递给pig_latin2(),我的所有单词的值都为None,程序崩溃。

# 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()

1 个答案:

答案 0 :(得分:0)

您没有捕获pig_latin2函数的返回值。所以无论这个功能做什么,你都要丢弃它的输出。

将此行修复为pig_latin函数:

if test not in BEGINNING:
    string = pig_latin2(transfer_word)  # <----------- forgot 'string =' here

如果这样修复,它对我有用。话虽如此,仍然会有很多东西需要清理。