我正在制作一个密码程序,稍后我将使用不同的密码填充它,但现在我有摩尔斯电码。我还不是最擅长编程的,所以下面的代码可能看起来过多,很容易分解成更好的代码。
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,它将输出.-.-.--
. 为什么会这样?我该如何解决?