Python Forum
morse code assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
morse code assignment
#1
Hello again and as always thank you for your continued help.I have reached out to the instructor but have not gotten any reply.
here is my issue. I have added all steps because i feel they all go together. I will post my code for step one
Step 1

In this step, you'll set up the logic to convert letters to Morse code.

Create an application that asks the user to enter an upper case letter.
Use an if-elif construct to determine which letter was entered and store the appropriate Morse code string in a variable. If there is no match, set the variable to "Unknown".
As an example, the Morse code for the letter 'S' is represented using a string of three periods, "...", for dot dot dot.
The Morse code for "O" is represented by three hyphens, "---", for dash dash dash.
Display the variable.
Save the program as step_1.py.
intro='''
This program will convert your text into morse code.
'''
print (intro)
Letter = input("Please enter an upper case letter or series of letters:")
if Letter == "A":
    print(".-")
elif Letter == "B":
    print("-...")
elif Letter == "C":
    print("-.-.")
elif Letter == "D":
    print("-..")

#list of Morse code to build program on
CODE = {'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': '----.' 
        }
Step 2

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.

I am strugling with step 2 i can not figure outwhat function to use or how to get my if and elif into it
Reply
#2
Going back over the assignment I really messed it up. It got the desired result but my code was not per instructions below is what i think step 1 should be but im getting an error.
def code(letter):
    if letter == A:
        code = ".-"
    elif letter == B:
        code = "-..."
    elif letter == C:
        code = "-.-."
    elif letter == D:
        code = "-.."
    return code

letter =(input("Please enter upper case letter to convert to morse code: "))
code = code(letter)
print("Your code for {} is {}.".format(letter,code))
This is the error:
Please enter upper case letter to convert to morse code: A
Traceback (most recent call last):
File "C:/Users/raymond/Documents/School/Python/week 3/step_1.py", line 17, in <module>
code = code(letter)
File "C:/Users/raymond/Documents/School/Python/week 3/step_1.py", line 6, in code
if letter == A:
NameError: name 'A' is not defined
Reply
#3
You want to put the letters in quotes, like you had them before.

There is a much easier way to do this with the dict you posted. However, the assignment does say to use if/elif to do the assignment. I'm going to assume that you will learn the easier way next, but I can show it to you if you want.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Jul-28-2019, 09:52 PM)ichabod801 Wrote: You want to put the letters in quotes, like you had them before.

There is a much easier way to do this with the dict you posted. However, the assignment does say to use if/elif to do the assignment. I'm going to assume that you will learn the easier way next, but I can show it to you if you want.

I am confused enough...lol I put the quotes and still getting errors

yes, the quotes worked thank you very much. I had something else mixed up from typing and retyping Now I'm on to step 2 and my first post applies.
Reply
#5
Can you restate your code and problem? Your first post says:

(Jul-28-2019, 07:32 PM)raymond2688 Wrote: I am strugling with step 2 i can not figure outwhat function to use or how to get my if and elif into it

You created the function and moved the if and elif into it in your second post. What other problem are you having?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
I am on step 2 and 3 I thought I had 2 done but I have errors
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.
STEP 2
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.

def text_to_morse(letter):
    if letter == "A":
        code = ".-"
    elif letter == "B":
        code = "-..."
    elif letter == "C":
        code = "-.-."
    elif letter == "D":
        code = "-.."
    else:
        print("Unknown Text")    
    return code

letter =(input("Please enter upper case letter to convert to morse code: "))
#code = code(letter)
text_to_morse = code(letter)
print("Your code for {} is {}.".format(letter,code))
ERROR for step 2
Traceback (most recent call last):
File "C:\Users\raymond\Documents\School\Python\week 3\step_2.py", line 20, in <module>
text_to_morse = code(letter)
NameError: name 'code' is not defined

STEP 3
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.

def code(letter):
    if letter == "A":
        code = ".-"
    elif letter == "B":
        code = "-..."
    elif letter == "C":
        code = "-.-."
    elif letter == "D":
        code = "-.."
    else:
        print("Unknown Text")    
    return code
for code in letter
letter =(input("Please enter upper case letter to convert to morse code: "))
code = code(letter)
print("Your code for {} is {}.".format(letter,code))
On step 3 I can seem to figure out the for command

any help will be greatly appreciated as always.
Reply
#7
The error you are getting in step 2 is because of the else clause, where you print 'Unknown Text'. You don't assign anything to code in that case. So when you try to return code on line 12, you haven't created anything named code, and Python gets confused. In that case I would set code to a dummy value, like an empty string or '???'.

One step three, you have three different things named code: the function on line 1, the loop variable on line 13, and the morse code value returned on line 15. Each time you assign the name code to something, you lose what it was assigned to before. So all three of those things need different names.

Note that the for variable should not be looping over letter. The instructions say to loop four times. You do that by looping over range(4). You also need a colon at the end of the for loop on line 13, and you need to indent each line of code that should be repeated after the for statement.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
I got step 2 to work properly thank you. I am on step 3 and I THINK I did the changes but am not confident they are what you meant. also I get and error that i am unsure of
Traceback (most recent call last):
File "C:/Users/raymond/Documents/School/Python/week 3/step_3.1.py", line 17, in <module>
for code_1 in letter:
NameError: name 'letter' is not defined

def text_to_morse(letter):
    if letter == "A":
        code = ".-"
    elif letter == "B":
        code = "-..."
    elif letter == "C":
        code = "-.-."
    elif letter == "D":
        code = "-.."
    else:
        print("Unknown Text")    
    return code
for code_1 in letter:
    letter = (input("Please enter upper case letter to convert to morse code: "))
    code_2 = text_to_morse(letter)
print("Your code for {} is {}.".format(letter,code))

        
Reply
#9
In the assignment you are instructed to get a letter and translate it 4 times. Your for loop should reflect that. At the time the for loop is executed, letter has not been defined. Instead you want to count 0 to 3 (or 1 to 4, whatever).
Reply
#10
If you do what jefsummers said, which is what I was talking about with the range(4), it will solve that problem with letters.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Morse Assignment Cambridge 3 2,427 Feb-02-2021, 02:40 AM
Last Post: Larz60+
  Write pseudo code for a class assignment Scrimshot 3 3,390 May-07-2019, 05:38 PM
Last Post: Scrimshot
  Need some help with a bit of code for an assignment. JackMercer50 1 2,280 Feb-09-2019, 04:13 PM
Last Post: stullis
  Need help with lists to continue my assignment code tinabina22 9 10,418 Oct-12-2016, 12:20 AM
Last Post: Yoriz
  [split] Need help with lists to continue my assignment code cylandur 7 9,454 Oct-11-2016, 03:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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