0
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()
你有沒有嘗試在調試模式下逐步執行代碼?這應該有助於您自己查找並修復錯誤。 –
我很努力使調試器的意義將嘗試太工作了。 – Ruark
如果我輸入:玩石頭退出,即任何將匹配我的程序工作的BEGINNINGS的單詞。如果我輸入:image away,即任何需要pig_latin2()的單詞,我會返回None。但我已經告訴python返回字符串。 DEBUGGER:word = {NoneType}無 – Ruark