2011-03-05, 08:22 AM
You don't really need an index for a string in python unless you're going to be doing string splitting:
Much better:
Also, make sure you have the parenthesis in there.
word[0].lower()
Otherwise you're going to receive the memory address for the method.
Lastly, you don't need to import string to get access to the lower method. That's a built-in.
Code:
x='spam'
for eachLetter in x:
print(eachLetter)Code:
s
p
a
mCode:
from string import *
word = raw_input("Enter the word you would like to have translated: ")
if word[0].lower == "a" or word[0].lower == "e" or word[0].lower == "i" or word[0].lower == "o" or word[0].lower == "u":
word = word + "-" + "ay"
print word
elif word[0].lower == "y":
if word[1] != "a" and word[1] != "e" and word[1] != "i" and word[1] != "o" and word[1] != "u":
word = word + "-" + "ay"
print word
elif word[1] == "a" or word[1] == "e" or word[1] == "i" or word[1] == "o" or word[1] == "u":
word = word[1:] + "-" + word[0] + "ay"
print word
elif word[0].lower != "a" or word[0].lower != "e" or word[0].lower != "i" or word[0].lower != "o" or word[0].lower != "u":
word = word[0:] + "-" + word[0] + "ay"
print wordMuch better:
Code:
def IsVowel(letter):
return letter in 'aeiouAEIOU'
word = raw_input("Enter the word you would like to have translated: ")
if IsVowel(word[0]):
word += '-ay'
elif word[0] in 'yY':
if IsVowel(word[1]): #Always try to assert POSITIVES in code. This makes it easier to debug later.
word = word[1:] + "-" + word[0] + "ay"
else:
word += '-ay'
else:
word += '-' + word[0] + 'ay'
print(word)Code:
>>>
Enter the word you would like to have translated: Sunday
Sunday-Say
>>>
Enter the word you would like to have translated: Forget
Forget-Fay
>>>
Enter the word you would like to have translated: Yesterday
esterday-Yay
>>>Also, make sure you have the parenthesis in there.
word[0].lower()
Otherwise you're going to receive the memory address for the method.
Code:
>>> word = 'X'
>>> word[0].lower
<built-in method lower of str object at 0x01DA7460>
>>> word[0].lower()
xLastly, you don't need to import string to get access to the lower method. That's a built-in.
