How would I turn a number into denary from binary and whats wrong with the code:
print (" Enter a number")
realnum = int(input())
print("In Binary that is", bin(realnum)[2:])
input(" Press enter to finish ")
How would I turn a number into denary from binary and whats wrong with the code:
print (" Enter a number")
realnum = int(input())
print("In Binary that is", bin(realnum)[2:])
input(" Press enter to finish ")
print("In Binary that is", bin(realnum)[2:])
print("In Denary that is", realnum)
是你想要的我想...根据我如何解释你的问题
看起来您正在尝试将输入转换为二进制。如果是这样,您可以使用format
:
>>> # Get the input as an integer
>>> realnum = int(input())
123
>>> # Make it binary
>>> format(realnum, '08b')
'01111011'
>>> x = format(realnum, '08b')
>>> # Put it back as an integer
>>> int(x, 2)
123
>>>
因此,您的代码变为:
print(" Enter a number")
realnum = int(input())
print("In Binary that is", format(realnum, '08b'))
print("In Denary that is", realnum)
input(" Press enter to finish ")