如何在 while 循环 运行 时保存每个字母
How to save each letter while the while loop is running
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:
这是我遇到问题的地方。我需要在元音(或我尝试过的 g)之前保存每个字母。这是猪拉丁翻译的开端。在这个阶段,我试图翻转前缀和单词的其余部分。
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
我正在翻转第一个元音之前的字母,以及单词的其余部分。
看起来像是 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
循环之外定义一个全局变量,你可以保存到。
如果你熟悉正则表达式,你可以使用它,或者你可以像这样以非常简单粗暴的方式编辑你的代码。
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]
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:
这是我遇到问题的地方。我需要在元音(或我尝试过的 g)之前保存每个字母。这是猪拉丁翻译的开端。在这个阶段,我试图翻转前缀和单词的其余部分。
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
我正在翻转第一个元音之前的字母,以及单词的其余部分。
看起来像是 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
循环之外定义一个全局变量,你可以保存到。
如果你熟悉正则表达式,你可以使用它,或者你可以像这样以非常简单粗暴的方式编辑你的代码。
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]