【发布时间】:2020-04-29 19:30:41
【问题描述】:
我正在尝试构建双向莫尔斯电码翻译器作为 Python 中的初学者项目,方法是使用字典将每个莫尔斯电码字符与其莫尔斯序列进行比较。 decryption 选项特别麻烦,因为我当前的解密代码构建为接收每个序列的每个单独字符,直到它读取一个空格,此时它意味着打印对应于的字母序列,然后清除空格后所有内容的序列跟踪器。在所有情况下,解密都不会输出任何内容,直接从接受输入跳到假设解密已经结束并返回菜单,而不实际输出任何序列在用户输入中的字母。
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()
【问题讨论】:
-
if digit == "." or digit == "-":或if digit in (".", "-"):或if digit in ".-": -
您当前的代码工作方式类似于
if (digit == ".") or ("-"):和"-"总是提供True所以你有if (digit == ".") or True:工作方式类似于if True:
标签: python dictionary spyder