2017-02-22 114 views
-3

我正在製作豬拉丁文程序。 首先,我需要在一個列表,它是定義VOWELS:for豬拉丁文

vowel = ['a','e','i','o','u'] 

但是對於我的家庭作業的要求,我需要一個無限循環)。當輸入'exit'時,程序停止。

而且,我需要使用在轉換部分時(提示:for x in VOWELS)名單

所以,我怎麼用這個循環?

如果我使用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循環來比較first_letter in vowel

+0

小心碰到刪除哎呦。把你的字符串當作一個字符列表。在for循環中使用它。沒有我們做你的功課,這應該會給你一個正確的方向推動 – jarcobi889

+0

這不是典型的豬拉丁文。一般而言,在第一個元音之前刪除字母,將它們粘貼到單詞的末尾,然後添加「ay」,但這不是您正在做的。澄清,拜託? – Prune

+0

請說明您對** for循環的需求。對於你給我們展示的內容,一個** for **循環會很愚蠢:你在**運算符中使用**是正確的。「公約」部分是什麼意思?這不是一個典型的編程概念。 – Prune

回答

1

要改變循環進入一個無限循環,也許你應該使用下面的?

while True: 
    text = input("Enter something") 
    if text == "exit": 
     break 

注意,這兩個迭代和很好的工作在一個字符串作爲一個列表:

vowel = "aeiou" 
... 
if first_letter in vowel: 
    ... 

我會更新這個關於該循環當你解釋的必要性更清楚。

2

如何使用for循環比較元音中的first_letter

For循環用於迭代序列,例如一個字符序列的字符。所以,如果你想遍歷一個單詞說,你可以做這樣的事情。

word = "stackoverflow" 
for charcter in word: 
    print(charcter) 

如果你想遍歷一個句子的所有單詞,你可以做這樣的事情。

sentence = "Welcome to stackoverflow, my friend" 
for word in sentence.split(): 
    print(word) 

sentence.split() - 根據空格將句子拆分爲單詞。


對於Pig Latin,我相信你需要這樣的事情。

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 
+0

@ user7607794我的回答對你有幫助嗎?如果是的話,你可以接受它作爲答案! –