0
alpha = ["A", "B", "C"]
morsecode = ["*-", "-***", "-*-*"]

string = raw_input(">> ")
list1 = list(string)

a1 = list1[0]

try:
   if a1 in alpha:
        a11 = alpha.index(a1)
        b1 = morsecode.index(a11)
        a1 = b1
        print a1

i want you to be able to type in "A" and it will print "*-"

jayelm
  • 7,236
  • 5
  • 43
  • 61
Slushy_G
  • 137
  • 2
  • 9
  • 6
    Why don't you just use a dictionary? Make "alpha" the key, and "morsecode" the value. – Cory Kramer Jan 20 '14 at 21:50
  • See the answer by iCodez. This is a very straight forward implementation of a dictionary. I suggest you read some introductory Python text as this is a very fundamental data structure. – Cory Kramer Jan 20 '14 at 21:54

3 Answers3

1

i want you to be able to type in "A" and it will print "*-"

What you want to do can be accomplished quite easily with a dictionary:

>>> dct = {
...     "A" : "*-", 
...     "B" : "-***", 
...     "C" : "-*-*"
... }
>>> string = raw_input(">> ")
>> A
>>> print dct[string]
*-
>>>
0

I think OP wants to handle more than a single character as input:

morse_code_dict = {"A": "*-", "B": "-***", "C": "-*-*"}

string = raw_input(">> ")

for char in string:
    print morse_code_dict[char],
dursk
  • 4,435
  • 2
  • 19
  • 30
0

I agree that a dictionary is the proper data structure for this.

I you want to use a list, you could do something along these lines:

morsecode = ["*-", "-***", "-*-*"]

ch = raw_input(">> ")

try:
    print '"{}" in morses code:"{}"'.format(ch, morsecode[ord(ch)-ord('A')])
except IndexError:
    print 'no code for: "{}"'.format(ch)
dawg
  • 98,345
  • 23
  • 131
  • 206