【问题标题】:Python 3 - A simple text string from user input to hexPython 3 - 从用户输入到十六进制的简单文本字符串
【发布时间】:2016-08-29 17:17:13
【问题描述】:

我希望我的代码接受用户输入,然后将其转换为十六进制

hex_name = input("Would you like to convert to hex?,Type Yes or No: ")


if hex_name == 'Yes' or hex_name == 'yes' or hex_name == 'YES':
        hex_input = input("Enter String value here:")
        h_input = binascii.hexlify(b'hex_input')
        print(hex_input, "Converts to: ", h_input)

        #when user enters no
elif hex_name == 'No' or hex_name == 'no' or hex_name == 'NO':
    print("You said", hex_name, "have a nice day!")

    #when user enters an empty space or hits the enter-bar
elif hex_name == '' or hex_name == ' ' or hex_name == '  ':
    print("Empty value detected, please try again")

#Ends program if user enters anything than a yes or no
elif  hex_name != 'Yes' or hex_name != 'yes':
        print("Sorry", hex_name, "is not a valid input")
        print("Please enter Yes or No, ", "have a nice day!")

'''代码有效,但它打印的是 hex_input 的十六进制值而不是用户输入,我做错了什么?'''

我对 python 很陌生,我使用 python 3.3.2

【问题讨论】:

  • 你能举一个你想给出的输入和你期望的输出的例子吗?
  • 考虑测试hex_name,将其转换为更低,例如if hex_name.lower() == 'yes': 避免不确定的情况——离题但有用的信息
  • 这里并不重要,但请注意hex_name != 'Yes' or hex_name != 'yes' 总是评估为True。否定布尔表达式时使用De Morgan's Law
  • 你传递了一个字符串 hexlify !而是将变量传递给它。

标签: python python-3.3


【解决方案1】:

去掉hex_input周围的引号:

binascii.hexlify(bytes(hex_input))

【讨论】:

    【解决方案2】:

    您是专门传递字符串“hex_input”,而不是变量 hex_input。这就是为什么它给你字符串“hex_input”的十六进制值。

    您必须删除“hex_input”周围的引号:

    if hex_name == 'Yes' or hex_name == 'yes' or hex_name == 'YES':
            hex_input = input("Enter String value here:")
            h_input = binascii.hexlify(b'hex_input')
                                        ^         ^
                                    these need to be removed
    

    所以该行应该是:

    h_input = binascii.hexlify(bytes(hex_input))
    

    如果你在某些东西周围加上引号,Python 会将引号之间的内容视为字符串。 hex_input 的值已经是一个字符串,所以你可以直接传递hex_input

    【讨论】:

      【解决方案3】:

      只需删除 hex_input 周围的引号即可。

      由于您是 python 的新手,请注意您可以轻松提高代码的质量和可读性。以下只是它的开始!

      hex_name = input("Would you like to convert to hex?,Type Yes or No: ")
      hex_name = hex_name.strip().lower()
      
      if hex_name:
          if hex_name not in ["yes", "no"]:
              print("Sorry", hex_name, "is not a valid input")
              print("Please enter Yes or No, ", "have a nice day!")
          elif hex_name == "yes":
              hex_input = input("Enter String value here:")
              h_input = binascii.hexlify(bytes(hex_input))
              print(hex_input, "Converts to: ", h_input)
          elif hex_name == "no":
              print("You said", hex_name, "have a nice day!")
      else:
          print("Empty value detected, please try again")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-02
        • 2012-08-15
        • 1970-01-01
        • 2017-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        相关资源
        最近更新 更多