我正在研究猪拉丁语翻译计划:
我的程序满足1-3就好了,但我似乎无法弄清楚如何不断提示用户输入一个单词,除非他们输入某种形式的"退出"。我知道我需要使用while语句,但这是我提出的最好的:
while True:
VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
statement = input("Please enter a word: ")
statementL = statement.lower()
if statementL == "quit":
print ("Exiting program")
break
def find_vowel(word):
for i in range(len(word)):
if word[i] in VOWELS:
return i
return -1
words = statement.split()
count = 0
for word in words:
vowel = find_vowel(word)
# No vowels found
if(vowel == -1):
print (word)
# A vowel is the first letter
elif(vowel == 0):
print (word + "way")
else:
# A consonant is first
print (word[vowel:] + word[:vowel] + "ay")
我当前的while循环不断提示用户输入一个单词但不翻译它,当我尝试将程序块退出到底部时出现错误。有什么建议?提前谢谢。
答案 0 :(得分:0)
我不太清楚问题是什么。我为角色复制了你的代码字符并且它正确运行。可能存在的唯一问题是您认为在while循环中的代码可能不是。检查你的缩进。这是我运行的代码:
while True:
VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")
statement = input("Please enter a word: ")
statementL = statement.lower()
if statementL == "quit":
print ("Exiting program")
break
def find_vowel(word):
for i in range(len(word)):
if word[i] in VOWELS:
return i
return -1
words = statement.split()
count = 0
for word in words:
vowel = find_vowel(word)
# No vowels found
if(vowel == -1):
print (word)
# A vowel is the first letter
elif(vowel == 0):
print (word + "way")
else:
# A consonant is first
print (word[vowel:] + word[:vowel] + "ay")