无法将键与用户在 Python 中输入的值进行比较

Having trouble comparing keys to values given by user input in Python

我正在尝试构建双向莫尔斯电码翻译器作为 Python 中的初学者项目,方法是使用字典将每个莫尔斯电码字符与其莫尔斯序列进行比较。 decryption 选项特别带来麻烦,因为我当前的解密代码被构建为接收每个序列的每个单独字符,直到它读取 space,此时它是意味着打印与序列对应的字母,然后清除 space 之后所有内容的序列跟踪器。在所有情况下,解密都不会输出任何内容,直接从接受输入跳到假设解密已经结束并返回菜单,而不会实际输出任何其序列在用户输入中的字母。

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()

我对循环做了一些更改:

 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()