Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Morse Assignment
#1
Step 2 (40 points):

In this step, you'll create a function to do the translation. This will make it easy to reuse the code for the if-elif logic without having to make lots of copies later on.

Copy step_1.py into a new program and call it step_2.py
Create a function named text_to_morse. The function should have one parameter, which contains the letter to be translated. Note that the function should be located at the top of the program just underneath the header comments.
Move just the if-elif code into the function, remembering to indent it. But, do not include the input statement or the print statement.
Add a return statement to the function that returns the variable containing the result.
After the input statement, add a call to the function using the input variable as the argument to the function.
Store the value returned by the function in a variable and display it.
Step 3 (20 points):

Here's where you put it all together. You'll be translating several Morse codes but instead of making copies of the if-elif logic for each translation, instead you will call the text_to_morse function.

Create a program called step_3.py.
Copy your text_to_morse function from step 2 to the top of the program, just under the header comments.
Do the following four times in a row:
Ask the user to enter a text character to be converted to Morse code.
Call the text_to_morse function to get the equivalent Morse code.
Display the text letter and its equivalent Morse code.



Step 2 Issues: I'm missing a step for the encryption for the code to actually send the readout of the morse code. I've been back and forth but am confused about what I am missing. Could anyone please help guide me, please? I do know that the if/else is a horrible way to define the function but that's what the homework calls for to do the assignment.

def text_to_morse(text):
    if text == "A":
        code = ".-"
    elif text == "B":
        code = ("-...")
    elif text == "C":
        code = ("-.-.")
    elif text == "D":
        code = ("-..")
    elif text == "E":
        code = (".")
    elif text == "F":
        code = ("..-.")
    elif text == "G":
        code = ("--.")
    elif text == "H":
        code = ("....")
    elif text == "I":
        code = ("..")
    elif text == "J":
        code = (".---")
    elif text == "K":
        code = ("-.-")
    elif text == "L":
        code = (".-..")
    elif text == "M":
        code = ("--")
    elif text == "N":
        code = ("-.")
    elif text == "O":
        code = ("---")
    elif text == "P":
        code = (".--.")
    elif text == "Q":
        code = ("--.-")
    elif text == "R":
        code = (".-.")
    elif text == "S":
        code = ("...")
    elif text == "T":
        code = ("-")
    elif text == "U":
        code = ("..-")
    elif text == "V":
        code = ("...-")
    elif text == "W":
        code = (".--")
    elif text == "X":
        code = ("-..-")
    elif text == "Y":
        code = ("-.--")
    elif text == "Z":
        code = ("--..")
    else:
        print("Unknown")
    code = ""
    return code
  
text =(input("Please enter upper case letter to convert to morse code: "))
code = text_to_morse(text)
print("Your code for {} is {}.".format(text,code))


Step 3 I figure I would do something along these lines, however, I'm still stuck on the encryption method I know I'm drawing a blank with.

elif text == "W":
        code = (".--")
    elif text == "X":
        code = ("-..-")
    elif text == "Y":
        code = ("-.--")
    elif text == "Z":
        code = ("--..")
    else:
        print("Unknown")
    code = ""
    return code
for text in range(4): 
    text =(input("Please enter upper case letter to convert to morse code: "))
    code = text_to_morse(text)
    print("Your code for {} is {}.".format(text,code))
Reply
#2
Indent code = "" at the end of the function. Currently it undoes anything done by the if statement.
Cambridge likes this post
Reply
#3
(Feb-02-2021, 02:32 AM)deanhystad Wrote: Indent code = "" at the end of the function. Currently it undoes anything done by the if statement.

Thank you, feel rather dumb that I've been fighting this over not having my indent correct. Everything else seems to work correctly with that change. Would you happen to see anything else that could be improved on based on the questions asked?
Reply
#4
you can use a dictionary to get your code conversion.
morse = {
    'A': '.-',
    'B': '_...',
    'C': '_._.',
    'D': '_..',
    'E': '.',
    'F': '.._.',
    'G': '_..'
    # etc
}

def translate_word(word):
    code = []
    word = word.upper()
    for letter in word:
        code.append(morse[letter])
    return code

def main():
    print(translate_word('AbCde'))

if __name__ == '__main__':
    main()
Output:
['.-', '_...', '_._.', '_..', '.']
Cambridge likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  morse code assignment raymond2688 11 8,722 Jul-29-2019, 07:43 PM
Last Post: raymond2688

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020