Python Forum
morse code assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
morse code assignment
#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


Messages In This Thread
morse code assignment - by raymond2688 - Jul-28-2019, 07:32 PM
RE: morse code assignment - by raymond2688 - Jul-28-2019, 09:38 PM
RE: morse code assignment - by ichabod801 - Jul-28-2019, 09:52 PM
RE: morse code assignment - by raymond2688 - Jul-28-2019, 09:57 PM
RE: morse code assignment - by ichabod801 - Jul-28-2019, 10:51 PM
RE: morse code assignment - by raymond2688 - Jul-29-2019, 12:37 PM
RE: morse code assignment - by ichabod801 - Jul-29-2019, 02:00 PM
RE: morse code assignment - by raymond2688 - Jul-29-2019, 05:02 PM
RE: morse code assignment - by jefsummers - Jul-29-2019, 05:20 PM
RE: morse code assignment - by ichabod801 - Jul-29-2019, 06:13 PM
RE: morse code assignment - by raymond2688 - Jul-29-2019, 06:20 PM
RE: morse code assignment - by raymond2688 - Jul-29-2019, 07:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Morse Assignment Cambridge 3 2,540 Feb-02-2021, 02:40 AM
Last Post: Larz60+
  Write pseudo code for a class assignment Scrimshot 3 3,537 May-07-2019, 05:38 PM
Last Post: Scrimshot
  Need some help with a bit of code for an assignment. JackMercer50 1 2,348 Feb-09-2019, 04:13 PM
Last Post: stullis
  Need help with lists to continue my assignment code tinabina22 9 10,687 Oct-12-2016, 12:20 AM
Last Post: Yoriz
  [split] Need help with lists to continue my assignment code cylandur 7 9,721 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