-4
我有一个项目,我必须将二进制文件翻译为十六进制。和小数如何在Python中使用if/else语句(请勿关闭)
这里是代码,它可能不是那么漂亮的任何帮助,非常感谢: 事情是,当我把1或0它仍然给我︰有一个错误,请重新键入你的二进制数。从我的代码
binary = raw_input("What Binary up to 8-bits would you like to use?")
if len(binary) <= 8:
for b in range(1,len(binary)+1):
if b ==2 or b==3 or b==4 or b==5 or b==6 or b==7 or b==8 or b==9:
print "**There is an error please re-type your binary number. (one's and zero's)**"
else:
print "Your code is too long! it needs to be 8 or less characters to
proccess!"
python已经具有这种内建 - 你可以使用任何你想要的基础将字符串转换为整数。例如,二进制的int(user_input,2)。看看这里:https://docs.python.org/3/library/functions.html#int – user2839978
@MooingRawr不,OP不会比较二进制。 –
如果'if'语句可以更简单地写为'if 2 <= b <= 9:'' – Barmar