Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python help thread?
#22
You don't really need an index for a string in python unless you're going to be doing string splitting:

Code:
x='spam'
for eachLetter in x:
  print(eachLetter)

Code:
s
p
a
m

Code:
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 word

Much 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()
x

Lastly, you don't need to import string to get access to the lower method. That's a built-in.
Reply


Messages In This Thread
Python help thread? - by Alley - 2011-02-12, 10:05 PM
Python help thread? - by Derosis - 2011-02-12, 10:09 PM
Python help thread? - by Erebus - 2011-02-12, 10:18 PM
Python help thread? - by XTOTHEL - 2011-02-12, 10:23 PM
Python help thread? - by Alley - 2011-02-12, 10:25 PM
Python help thread? - by XTOTHEL - 2011-02-12, 10:28 PM
Python help thread? - by Alley - 2011-02-12, 10:30 PM
Python help thread? - by XTOTHEL - 2011-02-12, 10:46 PM
Python help thread? - by Alley - 2011-02-12, 11:04 PM
Python help thread? - by XTOTHEL - 2011-02-12, 11:19 PM
Python help thread? - by Eos - 2011-02-12, 11:45 PM
Python help thread? - by XTOTHEL - 2011-02-12, 11:52 PM
Python help thread? - by Eos - 2011-02-13, 12:21 AM
Python help thread? - by XTOTHEL - 2011-02-13, 12:27 AM
Python help thread? - by Alley - 2011-02-13, 01:34 AM
Python help thread? - by Eos - 2011-02-13, 09:13 AM
Python help thread? - by ThatWasMyKil - 2011-02-16, 12:16 AM
Python help thread? - by AngelSL - 2011-03-01, 10:49 AM
Python help thread? - by EarthAdept2 - 2011-03-04, 04:53 PM
Python help thread? - by JPTheMonkey - 2011-03-04, 06:05 PM
Python help thread? - by EarthAdept2 - 2011-03-04, 06:09 PM
Python help thread? - by Fiel - 2011-03-05, 08:22 AM
Python help thread? - by JPTheMonkey - 2011-03-05, 09:38 PM
Python help thread? - by Fiel - 2011-03-06, 03:35 AM
Python help thread? - by EarthAdept2 - 2011-03-14, 02:19 AM
Python help thread? - by XTOTHEL - 2011-03-14, 02:30 AM
Python help thread? - by EarthAdept2 - 2011-03-14, 03:30 AM
Python help thread? - by JPTheMonkey - 2011-03-30, 07:44 PM
Python help thread? - by Fiel - 2011-03-30, 07:47 PM
Python help thread? - by JPTheMonkey - 2011-03-30, 10:16 PM
Python help thread? - by Alley - 2011-03-30, 11:58 PM
Python help thread? - by Fiel - 2011-04-01, 11:45 PM
Python help thread? - by Nikkey - 2011-04-02, 12:51 PM
Python help thread? - by JPTheMonkey - 2011-04-06, 11:46 PM
Python help thread? - by Alley - 2011-04-12, 10:58 PM
Python help thread? - by XTOTHEL - 2011-04-12, 11:13 PM
Python help thread? - by JPTheMonkey - 2011-04-12, 11:18 PM
Python help thread? - by Fiel - 2011-04-12, 11:20 PM
Python help thread? - by Alley - 2011-04-13, 12:50 AM
Python help thread? - by Fiel - 2011-04-13, 01:08 AM
Python help thread? - by Alley - 2011-04-13, 01:25 AM
Python help thread? - by Fiel - 2011-04-13, 01:31 AM
Python help thread? - by Alley - 2011-04-13, 01:34 AM
Python help thread? - by Fiel - 2011-04-13, 01:37 AM
Python help thread? - by Alley - 2011-04-13, 01:44 AM
Python help thread? - by Fiel - 2011-04-13, 01:59 AM
Python help thread? - by Alley - 2011-04-13, 02:02 AM
Python help thread? - by Fiel - 2011-04-13, 02:05 AM
Python help thread? - by Alley - 2011-04-13, 02:07 AM
Python help thread? - by JPTheMonkey - 2011-04-13, 02:27 AM
Python help thread? - by Alley - 2011-04-13, 10:17 AM
Python help thread? - by JPTheMonkey - 2011-04-13, 04:17 PM
Python help thread? - by EarthAdept2 - 2011-04-18, 07:01 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)