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。
# 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()发布于 2017-10-06 11:32:21
您没有捕获pig_latin2函数的返回值。所以不管这个函数做什么,你都要放弃它的输出。
修正pig_latin函数中的这一行:
if test not in BEGINNING:
string = pig_latin2(transfer_word) # <----------- forgot 'string =' here当它固定的时候,它对我起作用。话虽如此,还是有一堆东西要清理。
https://stackoverflow.com/questions/46604170
复制相似问题