【发布时间】: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