Issue
I'm attempting to build a both-ways morse code translator as a beginner project in Python by using a dictionary to compare each morse code character to its morse sequence. The trouble comes with the decryption option in particular, as my current decryption code is built to take in each individual character of each sequence until it reads a space, at which point it's meant to print the letter corresponding to the sequence, then clean out the sequence tracker for everything after the space. In all cases, the decryption instead outputs nothing, skipping straight from taking the input to assuming that decryption has ended and directing back to the menu, without actually outputting any of the letters whose sequences were in the user input.
morse_code_dict = {"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":"--..",
"1":".----", "2":"..---", "3":"...--", "4":"....-", "5":".....", "6":"-....",
"7":"--...", "8":"---..", "9":"----.", "0":"-----"}
def encrypt(morse_code_dict):
morse_code_current = ""
morse_code_current = input("Please enter all of the letters and numbers you want to have encrypted. Enter the letters in uppercase." + '\n')
for digit in morse_code_current:
if digit in morse_code_dict.keys():
print(morse_code_dict[digit])
print(" ")
print("Thank you! We'll now be bringing you back to the menu for further translation, or to exit.")
choice()
return
def decrypt(morse_code_dict):
morse_code_current = ""
morse_code_sequence = ""
morse_code_current = input("Hello! Please enter each morse code sequence you want turned into a character. Separate each individual sequence with a space." + '\n')
for digit in morse_code_current:
if digit == "." or "-":
morse_code_sequence += digit
elif digit == " ":
print(morse_code_dict.keys(morse_code_sequence))
morse_code_sequence = ""
else:
print("One or more of these morse code sequences is inaccurate. Please double-check and try again.")
decrypt(morse_code_dict)
morse_code_sequence = ""
print("Thank you! We'll now be bringing you back to the menu for further translation, or to exit.")
choice()
return
def choice():
input_tracker = ""
input_tracker = input("Enter your choice now." + '\n')
if input_tracker == "E":
encrypt(morse_code_dict)
elif input_tracker == "D":
decrypt(morse_code_dict)
elif input_tracker == "Q":
exit()
else:
print("Please enter a character corresponding to the options shown initially." + '\n')
choice()
print("Welcome to the both-ways morse code translator!")
print("Please enter 'E' if you want to encrypt a series of characters.")
print("Enter 'D' if you want to decrypt a morse code series.")
print("Enter 'Q' to exit the program.")
choice()
Solution
I've made a couple of changes to the loop:
morse_code_dict = {"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":"--..",
"1":".----", "2":"..---", "3":"...--", "4":"....-", "5":".....", "6":"-....",
"7":"--...", "8":"---..", "9":"----.", "0":"-----"}
def encrypt(morse_code_dict):
morse_code_current = ""
morse_code_current = input("Please enter all of the letters and numbers you want to have encrypted. Enter the letters in uppercase." + '\n')
for digit in morse_code_current:
if digit in morse_code_dict.keys():
print(morse_code_dict[digit])
print(" ")
print("Thank you! We'll now be bringing you back to the menu for further translation, or to exit.")
choice()
return
def decrypt(morse_code_dict):
morse_code_current = ""
morse_code_sequence = ""
morse_code_current = input("Hello! Please enter each morse code sequence you want turned into a character. Separate each individual sequence with a space." + '\n')
for digit in morse_code_current:
if digit == "." or digit == "-": # Need to specify the variable with each 'or' condition
morse_code_sequence += digit
elif digit == " ":
for letter, code in morse_code_dict.items(): # .items() and remove argument
# Have to loop the dictionary and search.
if code == morse_code_sequence:
print(letter)
morse_code_sequence = ""
else:
print("One or more of these morse code sequences is inaccurate. Please double-check and try again.")
decrypt(morse_code_dict)
morse_code_sequence = ""
print("Thank you! We'll now be bringing you back to the menu for further translation, or to exit.")
choice()
return
def choice():
input_tracker = ""
input_tracker = input("Enter your choice now." + '\n')
if input_tracker == "E":
encrypt(morse_code_dict)
elif input_tracker == "D":
decrypt(morse_code_dict)
elif input_tracker == "Q":
exit()
else:
print("Please enter a character corresponding to the options shown initially." + '\n')
choice()
print("Welcome to the both-ways morse code translator!")
print("Please enter 'E' if you want to encrypt a series of characters.")
print("Enter 'D' if you want to decrypt a morse code series.")
print("Enter 'Q' to exit the program.")
choice()
Answered By - Nebulous29
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.