Python 猪拉丁语中的for循环

Python 猪拉丁语中的for循环,python,Python,我在建一个拉丁猪节目。 首先,我需要在以下列表中定义元音: vowel = ['a','e','i','o','u'] 然而,我的家庭作业要求我需要一个无限循环)。当输入“退出”时,程序停止 此外,在转换部分,我需要使用列表(提示:表示元音中的x) 那么,如何使用这个for循环呢 如果我使用If语句,它可以正常工作 while text!=('exit'): ltext = text.lower() first_letter = word[0] if first_let

我在建一个拉丁猪节目。 首先,我需要在以下列表中定义元音:

vowel = ['a','e','i','o','u']
然而,我的家庭作业要求我需要一个无限循环)。当输入“退出”时,程序停止

此外,在转换部分,我需要使用列表(提示:
表示元音中的x

那么,如何使用这个for循环呢

如果我使用
If
语句,它可以正常工作

while text!=('exit'):
    ltext = text.lower()
    first_letter = word[0]
    if first_letter in vowel:
        new_word=ltext+'ay'
    else:
        new_word=ltext[1:]+first_letter+'ay'
    print(new_word)

但是我不知道如何使用
for
循环。那么,我如何使用
for
循环来比较元音中的
第一个字母呢?

要将while循环变成无限循环,也许您应该使用以下方法

while True:
    text = input("Enter something")
    if text == "exit":
        break
请注意,中的迭代和对字符串的作用与对列表的作用一样好:

vowel = "aeiou"
...
if first_letter in vowel:
    ...
当您更清楚地解释需求时,我将更新有关for循环的内容

如何使用for循环来比较元音中的
第一个字母

For循环用于在一个序列上进行迭代,例如,一个单词是一个字符序列。所以,如果你想重复一个单词,你可以这样做

word = "stackoverflow"
for charcter in word:
    print(charcter)
sentence = "Welcome to stackoverflow, my friend"
for word in sentence.split():
    print(word)
vowel = ['a','e','i','o','u']
while True: # infinite loop
    text = input("Give your input text: ")
    if text == "exit":
        break  # stops the loop
    else:
        if text[0].lower() in vowel:
            text = text + 'ay'
        else:
            if text[0].isupper():
                text = text[1].upper() + text[2:] + text[0].lower() + 'ay'
            else:
                text = text[1:] + text[0] + 'ay'
        print(text)
如果你想重复一个句子中的所有单词,你可以这样做

word = "stackoverflow"
for charcter in word:
    print(charcter)
sentence = "Welcome to stackoverflow, my friend"
for word in sentence.split():
    print(word)
vowel = ['a','e','i','o','u']
while True: # infinite loop
    text = input("Give your input text: ")
    if text == "exit":
        break  # stops the loop
    else:
        if text[0].lower() in vowel:
            text = text + 'ay'
        else:
            if text[0].isupper():
                text = text[1].upper() + text[2:] + text[0].lower() + 'ay'
            else:
                text = text[1:] + text[0] + 'ay'
        print(text)
-根据空格将句子拆分为单词


因为,我相信你需要这样的东西

word = "stackoverflow"
for charcter in word:
    print(charcter)
sentence = "Welcome to stackoverflow, my friend"
for word in sentence.split():
    print(word)
vowel = ['a','e','i','o','u']
while True: # infinite loop
    text = input("Give your input text: ")
    if text == "exit":
        break  # stops the loop
    else:
        if text[0].lower() in vowel:
            text = text + 'ay'
        else:
            if text[0].isupper():
                text = text[1].upper() + text[2:] + text[0].lower() + 'ay'
            else:
                text = text[1:] + text[0] + 'ay'
        print(text)
样本I/O:

Give your input text: pig
igpay
Give your input text: Latin
Atinlay
Give your input text: exit

意外地击中删除哎哟。将字符串视为字符列表。在for循环中使用它。这会让你在没有我们做作业的情况下朝着正确的方向前进这不是典型的猪拉丁语。一般来说,你去掉第一个元音前的字母,把它们钉在单词的末尾,然后加上“ay”,但这不是你要做的。请澄清?请澄清您对for循环的需求。对于您向我们展示的内容,For循环是愚蠢的:您使用in运算符是正确的。你所说的“公约部分”是什么意思?这不是一个典型的编程概念。@Prune sry对于拼写错误,转换过程中需要使用元音列表。您还没有阐明for循环的需要,或者为什么要使用这种奇怪的转换。例如,这将把输入“trash”改为“trashay”(应该是“ashtray”),而“easy”将变成“asyeay”。我真的知道你想做什么?常规转换将使用for循环,但要迭代单词的字母,而不是元音。@user7607794我的答案对您有帮助吗?如果是,你可以接受它作为答案!