def to_pig_latin(s):
j = 0 # points to first character in word
i = 0
new_sentence_1 = '' # variable to store strings being changed
vowel_position = 0 # show the position of the first vowel
number_of_words = 0
number_of_spaces = s.count(" ")
number_of_words = number_of_spaces + 1
space_position = s.find(" ") # find the position of the first space
sent = s[:space_position] # slice the first word of the sentence
old_sent = s[len(sent)+1:] # stores the old sentence without the first word of s
while number_of_spaces >= 0:
if sent[j] in ["a", "e", "i", "o", "u"]: # checks if first character is a vowel
new_sentence = sent + "way" # adds 'way' to the first word
new_sentence_1 = new_sentence_1 + ' ' + new_sentence # adds the words
else: # if first character is not equal to a vowel
for i in range(len(sent)):
# check to see if first character in s is a vowel
if s[i] == 'a':
break
if s[i] == 'e':
break
if s[i] == 'i':
break
if s[i] == 'o':
break
if s[i] == 'u':
break
vowel_position = i # takes position of first vowel reached in word
consonant_sequence = sent[:vowel_position] # stores all the consonants up to the first vowel, but not the first vowel
sent = sent[vowel_position:] # slices the word from the first vowel to the end
new_sentence = sent + 'a' + consonant_sequence + 'ay' # adds strings
new_sentence_1 = new_sentence_1 + ' ' + new_sentence # adds the words
s = old_sent # takes the value of old_sent
space_position = s.find(" ") # find the position of the first space
如果“s”中有一个单词,我如何更改下面的部分以便检查?或者,如果字符串's'中的最后一个单词以一个以一个或多个辅音开头的单词结尾?
if space_position == -1:
space_position = len(s)
sent = s[:space_position]
if sent[j] in ["a", "e", "i", "o", "u"]:
new_sentence = sent + "way"
new_sentence_1 = new_sentence_1 + ' ' + new_sentence
break
else:
for i in range(len(sent)):
if s[i] == 'a':
break
if s[i] == 'e':
break
if s[i] == 'i':
break
if s[i] == 'o':
break
if s[i] == 'u':
break
vowel_position = i
consonant_sequence = sent[:vowel_position]
sent = sent[vowel_position:]
new_sentence = sent + 'a' + consonant_sequence + 'ay'
new_sentence_1 = new_sentence_1 + ' ' + new_sentence
sent = s[:space_position]
old_sent = s[len(sent)+1:]
number_of_spaces = s.count(" ")
number_of_words = number_of_spaces + 1
return new_sentence_1[1:]
# test program for english/piglatin translator
import piglatin
choice = input ("(E)nglish or (P)ig Latin?\n")
action = choice[:1]
if action == 'E':
s = input("Enter an English sentence:\n")
new_s = piglatin.to_pig_latin(s)
print("Pig-Latin:")
print(new_s)
elif action =='P':
s = input("Enter a Pig Latin sentence:\n")
new_s = piglatin.to_english(s)
print("English:")
print(new_s)
(E)nglish或(P)ig Latin? E输入英文句子:apple Traceback (最近一次调用最后一次):文件 “/Applications/Wing101.app/Contents/Resources/src/debug/tserver/_sandbox.py” 第9行,在文件“/ Users / azhar / Desktop / Computer中 科学/作业/作业4(功能与弦乐)/piglatin.py“, 第44行,在to_pig_latin中 如果在[“a”,“e”,“i”,“o”,“u”]中发送[j]:builtins.IndexError:字符串索引超出范围
答案 0 :(得分:0)
你有一个for循环遍历所有数字,直到sent
的长度,但尝试索引到s
:
for i in range(len(sent)):
if s[i] == 'a':
break
如果len(s)< len(已发送)此for循环没有任何用处。