2021年2月5日星期五

My morse code function is only returning the first character of the string

I have a function thats supposed to convert a python string to morse code but it's only converting the first character of that string to morse code.

Here's what i've got so far:

def morse(message):        codes = {',': ' COMMA','.': ' STOP', '?': 'QUERY', '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': '--..'}    #Empty string holds the morse code as the string loops     morse_code = ""        for x in message:      morse_code += codes[x.upper()]                  return morse_code        message = input("Enter the text you would like to convert:")    print (morse(message))  

When I type a string Here's what I get;

Enter the text you would like to convert: python

and here's what is outputted: .--.


However; I want it to output the whole word for example: .--. '-.--' '-' .... ___ _.

https://stackoverflow.com/questions/66073215/my-morse-code-function-is-only-returning-the-first-character-of-the-string February 06, 2021 at 11:06AM

没有评论:

发表评论