Jul-29-2019, 12:37 PM
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.
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.
any help will be greatly appreciated as always.
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.