简体   繁体   English

你如何在 python 中读取和打印整个 sting

[英]how do you read and print entire sting in python

A friend and I is having some trouble with this code, When we run it and type a Morsecode, it prints only E and T, so it only reads the first char.. We have tried rewriting the DECODE segment cause we thought it might be that, but after 3 tries and multiple google searches.. We now ask you guys for help我和一个朋友在处理这段代码时遇到了一些问题,当我们运行它并输入 Morsecode 时,它​​只打印 E 和 T,所以它只读取第一个字符。我们尝试重写 DECODE 段,因为我们认为它可能是那个,但是经过 3 次尝试和多次谷歌搜索.. 我们现在向你们寻求帮助

    DECODE = {
    '.-':'A',       '-...':'B',     '-.-.':'C',
    '-..':'D',      '.':'E',        '..-.':'F',
    '--.':'G',      '....':'H',     '..':'I',
    '.---':'J',     '-.-':'K',      '.-..':'L',
    '--':'M',       '.-':'N',       '---':'O',
    '.--.':'P',     '--.-':'Q',     '.-.':'R',
    '...':'S',      '-':'T',        '..-':'U',
    '...-':'V',     '.--':'W',      '-..-':'X',
    '-.--':'Y',     '--..':'Z',

    '-----': '0',   '.----': '1',   '..---': '2',
    '...--': '3',   '....-': '4',   '.....': '5',
    '-....': '6',   '--...': '7',   '---..': '8',
    '----.': '9',   '//': ' ',

def main1():

    msg1 = raw_input('Input morsecode: ')

    for char in msg1:
        print DECODE[char.upper()],

if __name__ == "__main__":
    main1()

Thank谢谢

When you get the input you need to split it.当您获得输入时,您需要将其拆分。 Say, for example, the user inputed :比如说,用户输入:

.- .----

You need to split the spaces so you get a list of each morse code character :您需要拆分空格,以便获得每个莫尔斯电码字符的列表:

msgChars = msg1.split(" ")

That will output msgChars as a list of every character in the morse code :这将输出 msgChars 作为莫尔斯电码中每个字符的列表:

[".-", ".----"]

You should split the message.您应该拆分消息。 Here's an example in Python 3. Didn't have Python 2 readily available.这是 Python 3 中的一个示例。没有现成的 Python 2。 Sorry about that.对于那个很抱歉。

morse_map = {
    '.-':'A', '-..':'D', '--.':'G', '.---':'J', '--':'M', '.--.':'P', 
    '...':'S', '...-':'V', '-.--':'Y', '..---':'2', '...--':'3', 
    '-....':'6', '----.':'9', '-.-.':'C', '..-.':'F', '..':'I', 
    '.-..':'L', '---':'O', '.-.':'R', '..-':'U', '-..-':'X', 
    '-----':'0', '.....':'5', '---..':'8', '-...':'B', '.':'E', 
    '....':'H', '-.-':'K', '.-':'N', '--.-':'Q', '-':'T', '.--':'W', 
    '--..':'Z', '.----':'1', '....-':'4', '--...':'7', '//':' '
}

def main():
    msg = input('Input morsecode: ')

    for char in msg.split(' '):
        print(morse_map[char], end="")

if __name__ == "__main__":
    main()

Example input/output:示例输入/输出:

Input morsecode: . .. -... // . . .-.
EIB EER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM