Reputation: 161
So for example, if I input bob
, it should give me obb
. Likewise, something like plank
should give me ankpl
.
s = input("What word do you want translated?")
first = s[0]
vowel = "aeiou"
for i in range (1, len(s)):
if first in vowel:
s = s + "way"
print (s)
else:
s = s[1:] + s[0]
print (s)
This currently is only giving me lankp
for plank
.
Thanks!
Upvotes: 1
Views: 4859
Reputation: 9
This will work
#Move Consonants to the End
def MoveConsonants(input1):
contains_vowel = False
for letter in input1: #Check if the work contains a vowel or not
if letter in 'aeiou':
contains_vowel = True
#If the word doesn't contain a vowel, return the same word
if not contains_vowel:
return input1
#Check if the first letter is a vowel, if so, we can return the string
if input1[0] in 'aeiou':
return input1
#if the first letter is not a vowel, move the first letter to the end and repeat the process
input1 = input1[1:] + input1[0]
return MoveConsonants(input1)
print(MoveConsonants('Plank'))
Upvotes: 0
Reputation: 3009
It can actually be made much simpler:
s = raw_input("What word do you want translated?").strip()
vowel = set("aeiou")
if vowel & set(s):
while s[0] not in vowel:
s = s[1:] + s[0]
print s
else:
print "Input has no vowels"
Upvotes: 4
Reputation: 304355
The problem with your program is that the else
isn't indented to the correct level,so you have an for/else
construct instead of an if/else
.
Here is a more efficient approach.
if vowels = set("aeiou")
, you can get the position of the first vowel like this
next(i for i, j in enumerate(s) if j in vowels)
eg:
>>> s = "plank"
>>> vowels = set("aeiou")
>>> next(i for i, j in enumerate(s) if j in vowels)
2
and
>>> s[2:] + s[:2]
'ankpl'
So now you only need to manipulate the string once.
If there are no vowels, the code raises an exception instead of running forever :)
>>> s="why why why?"
>>> next(i for i, j in enumerate(s) if j in vowels)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Upvotes: 0
Reputation: 19810
It's probably a little more efficient to search for the vowel and then do the string rotate once:
vowels = 'aeiou'
for index, character in enumerate(s):
if character in vowels: break
s = s[index:] + s[:index]
Upvotes: 0
Reputation: 1239
You only set first = s[0]
once and that is done before the loop. You probably want to set it inside the for loop.
Upvotes: 1