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
我希望您能够输入“A”并打印“* - ”
答案 0 :(得分:1)
我希望您能够输入“A”并打印“* - ”
使用dictionary:
可以轻松完成您想要做的事情>>> dct = {
... "A" : "*-",
... "B" : "-***",
... "C" : "-*-*"
... }
>>> string = raw_input(">> ")
>> A
>>> print dct[string]
*-
>>>
答案 1 :(得分:0)
我认为OP想要处理多个字符作为输入:
morse_code_dict = {"A": "*-", "B": "-***", "C": "-*-*"}
string = raw_input(">> ")
for char in string:
print morse_code_dict[char],
答案 2 :(得分:0)
我同意字典是适当的数据结构。
我希望使用列表,你可以按照这些方式做点什么:
morsecode = ["*-", "-***", "-*-*"]
ch = raw_input(">> ")
try:
print '"{}" in morses code:"{}"'.format(ch, morsecode[ord(ch)-ord('A')])
except IndexError:
print 'no code for: "{}"'.format(ch)