【问题标题】:Having trouble comparing keys to values given by user input in Python在将键与 Python 中用户输入给出的值进行比较时遇到问题
【发布时间】: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


【解决方案1】:

我对循环进行了一些更改:

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

【讨论】:

  • 我在代码中实现了这个更改,但似乎没有什么不同。
  • 我已经编辑了上面的代码以包含所有内容。但是出于兴趣,您使用的是哪个 Python 版本?
  • 3.7;我使用 Spyder 作为我的开发环境。我实现了您所做的全部更改,但它仍然没有显示任何已翻译的字母,仍然没有打印出应有的翻译内容。
  • 嗯...只是为了保持一致性,您要翻译什么代码?这可能是一个或几个字母的问题,而不是逻辑问题?
  • 我正在尝试做的是在命令行上接收用户的输入,用户输入类似“.- -... -.-.”的内容,程序读取行并将每个字符复制到一个字符串中,直到读取一个空格,此时程序输出与该序列对应的字符。我举个例子的序列的最终结果最好是“A B C”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多