私は後で別の暗号を設定する暗号プログラムを作成していますが、今のところモールス符号があります。私はまだプログラミングが得意ではないので、次のコードは過剰に見えるかもしれません。
import os
def c():
os.system('cls')
doneOnce = False
while True:
print 'Code Maker Breaker!'
print '-------------------\n'
if doneOnce == False:
print 'Welcome to CMB!'
print 'In CMB you can pick several types of codes,'
print 'Then convert text into this code! If you want,'
print 'You can then convert text back into this code!'
print 'Use numbers to select different codes.\n'
doneOnce = True
print 'Codes! Please select with corresponding numbers.\n'
print '1. Morse'
code = raw_input('Enter the number for your code: ')
c()
if code == '1':
print 'The Morse code should be used with spaces'
print 'after every letter to avoid the wrong translation.\n'
print '1. Translate from English to -- --- .-. ... .'
print '2. Translate from -- --- .-. ... . to English'
morse = raw_input('Enter what you want: ')
c()
if morse == '1':
print 'Translating to Morse.'
print 'Enter your English sentence'
engtomorse = raw_input('>>> ').lower()
engtomorse = engtomorse.replace('a', '.-')
engtomorse = engtomorse.replace('b', '-...')
engtomorse = engtomorse.replace('c', '-.-.')
engtomorse = engtomorse.replace('d', '-..')
engtomorse = engtomorse.replace('e', '.')
engtomorse = engtomorse.replace('f', '..-.')
engtomorse = engtomorse.replace('g', '--.')
engtomorse = engtomorse.replace('h', '....')
engtomorse = engtomorse.replace('i', '..')
engtomorse = engtomorse.replace('j', '.---')
engtomorse = engtomorse.replace('k', '-.-')
engtomorse = engtomorse.replace('l', '.-..')
engtomorse = engtomorse.replace('m', '--')
engtomorse = engtomorse.replace('n', '-.')
engtomorse = engtomorse.replace('o', '---')
engtomorse = engtomorse.replace('p', '.--.')
engtomorse = engtomorse.replace('q', '--.-')
engtomorse = engtomorse.replace('r', '.-.')
engtomorse = engtomorse.replace('s', '...')
engtomorse = engtomorse.replace('t', '-')
engtomorse = engtomorse.replace('u', '..-')
engtomorse = engtomorse.replace('v', '...-')
engtomorse = engtomorse.replace('w', '.--')
engtomorse = engtomorse.replace('x', '-..-')
engtomorse = engtomorse.replace('y', '-.--')
engtomorse = engtomorse.replace('z', '--..')
engtomorse = engtomorse.replace('0', '-----')
engtomorse = engtomorse.replace('1', '.----')
engtomorse = engtomorse.replace('2', '..---')
engtomorse = engtomorse.replace('3', '...--')
engtomorse = engtomorse.replace('4', '....-')
engtomorse = engtomorse.replace('5', '.....')
engtomorse = engtomorse.replace('6', '-....')
engtomorse = engtomorse.replace('7', '--...')
engtomorse = engtomorse.replace('8', '---..')
engtomorse = engtomorse.replace('9', '----.')
engtomorse = engtomorse.replace('.', '.-.-.-')
engtomorse = engtomorse.replace(',', '--..--')
engtomorse = engtomorse.replace('?', '..--..')
engtomorse = engtomorse.replace('!', '..--.')
engtomorse = engtomorse.replace(':', '---...')
engtomorse = engtomorse.replace('"', '.-..-.')
engtomorse = engtomorse.replace("'", '.----.')
engtomorse = engtomorse.replace('=', '-...-')
c()
print 'Your sentence in Morse code is', engtomorse
print 'Press enter.'
raw_input()
elif morse == '2':
pass
else:
print 'You entered something wrong... Go back!'
print 'Press enter.'
raw_input()
else:
print 'What?'
print 'Press enter.'
raw_input()
c()
さて、それはたくさんあります。しかし、私にとってうまくいかない部分は、ユーザーが文を入力すると、プログラムがすべての文字をモールス符号に置き換えます。印刷すると、奇妙な答えが返ってきます。たとえば、文を A と入力すると、 が出力されます.-.-.--
。なぜこうなった?どうすれば修正できますか?