2015-04-07 73 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循环。