0

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 ") 
4

2 回答 2

2
print("In Binary that is", bin(realnum)[2:])
print("In Denary that is", realnum)

是你想要的我想...根据我如何解释你的问题

于 2013-11-05T20:31:54.327 回答
1

看起来您正在尝试将输入转换为二进制。如果是这样,您可以使用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 ") 
于 2013-11-05T20:32:14.897 回答