2015-04-07 74 views
0
word = raw_input('Enter a word: ') 

for i in word: 
    if i in ['a','e','i','u','A','E','I','O','o','U']: 
     word1 = i 
     break 

    else: 
     print i, 


print "" 

i = 0 
while word[i]!=word1: 

這是我遇到問題的地方。我需要保存元音前的每個字母(或者我已經嘗試過的)。這是一個拉丁文翻譯的開始。在這個階段,我試圖翻轉前綴和其他字詞。如何在while循環運行時保存每個字母

g = word[i] 
    i = i+1 


prefix = g + word1 

print prefix 

例子:

input - person 
output - rsonpe 

input - hello 
output - llohe 

input - pppat 
output - tpppa 

input - hhhhhelllllloool 
output - llllllooolhhhhhe 

我翻轉字母的第一個元音之前,和該單詞的其餘部分。

+0

你試過追加到列表嗎? – reticentroot

+0

不,我可以在哪裏用.append來做這件事? – Ele

+0

在你的陳述。如果你寫出輸出的明確細節,你可能會更具體。 – reticentroot

回答

0

如果你熟悉它,你可以使用正則表達式,或者你可以用一種非常簡單粗糙的方式編輯你的代碼。

word = raw_input('Enter a word: ') 
word1 = 0 
for i in range(0,len(word)) : 
    if word[i] in ['a','e','i','u','A','E','I','O','o','U']: 
     word1=i 
     break 
print word[word1+1:]+word[:word1]+word[word1] 
+0

謝謝你的解決方案,但是有沒有一種方法可以解釋最後一行,並將一些元素保存到變量中,以便於閱讀? – Ele

+0

我只是在最後一行切分字符串。 python提供了一個非常簡單的方法來實現這一點。例如,你有一個字符串wish ='hello'在任何具體的索引中獲得任何charector你只需要[index] ex: - wish [0]會給你'h',如果你只想得到前3字符串中的字符,你可以使用wish [:3],它會給你索引0到3(0,1,2)的字符'hel'。同樣的方式希望[3:]會給你索引從4到字符串結束的字符。要了解更多關於切片字符串檢查http://www.pythoncentral.io/cutting-and-slicing-strings-in-python/ –

+0

而不是每次都創建一個變量。您可以將所需的元素保存在列表中通過創建一個列表並添加要存儲的字符。 –

0

看起來像regular expressions工作:

import re 

Vowel = re.compile('[aeiouAEIOU]') 

def flipper (inStr) : 
    myIndex = Vowel.search(inStr).start() + 1 
    return inStr[myIndex:] + inStr[:myIndex] 

flipper('hello') 

輸出:

'llohe' 

或者,如果你真的想用while循環做,你只需要到外面定義一個全局變量你可以保存的while循環。