Reputation: 47
I am helping a friend with Python and I might be getting confused with C++. So I am curious as to why this would not work. The expected function of this is a Pig Latin translator so if there is a vowel as the first letter 'ay' is added to the end but if the first letter is a consonant, that letter is added to the end and then the 'ay' is added. Example: apple --> appleay watch --> atchway Sorry I completely forgot to post the code (edit)
vowel = ['a','e','i','o','u']
counter = -1
while True:
text = input("Write a word to translate. If you do not want to play anymore,
write exit: ")
if text == "exit":
break
elif text[0].lower() in vowel:
text = text + 'ay'
print(text)
elif text[0].lower() not in vowel:
letter = text[0]
length = len(text) - 1
for i in range(1, length):
text[i-1] = text[i]
text[length + 1] = letter
print(text)
Upvotes: 0
Views: 41
Reputation: 107115
Strings are immutable, so you can't assign to a string at specific index like text[i-1] = text[i]
.
Instead, use text = text[1:] + text[0] + 'ay'
to do what you want:
vowel = ['a','e','i','o','u']
counter = -1
while True:
text = input("Write a word to translate. If you do not want to play anymore, write exit: ")
if text == "exit":
break
elif text[0].lower() in vowel:
text = text + 'ay'
print(text)
elif text[0].lower() not in vowel:
text = text[1:] + text[0] + 'ay'
print(text)
Upvotes: 1