morse code assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: morse code assignment (/thread-20120.html) Pages:
1
2
|
morse code assignment - raymond2688 - Jul-28-2019 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 RE: morse code assignment - raymond2688 - Jul-28-2019 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 RE: morse code assignment - ichabod801 - Jul-28-2019 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. RE: morse code assignment - raymond2688 - Jul-28-2019 (Jul-28-2019, 09:52 PM)ichabod801 Wrote: You want to put the letters in quotes, like you had them before. 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. RE: morse code assignment - ichabod801 - Jul-28-2019 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? RE: morse code assignment - raymond2688 - Jul-29-2019 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. RE: morse code assignment - ichabod801 - Jul-29-2019 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. RE: morse code assignment - raymond2688 - Jul-29-2019 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)) RE: morse code assignment - jefsummers - Jul-29-2019 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). RE: morse code assignment - ichabod801 - Jul-29-2019 If you do what jefsummers said, which is what I was talking about with the range(4), it will solve that problem with letters. |