在这个 Python 代码中,我有以下问题。当显示结果时,我在结果中得到一堆零(见下文) - 否则,结果是正确的(字符串的结尾数字)。任何人都可以发现错误吗?
def menu():
binNumber = ''
decNumber = float(input("Enter a positive number: "))
decNumber, binNumber = decimalToBinary(decNumber, binNumber)
printResult(binNumber)
def decimalToBinary(dec, bin):
while dec != 0:
remain = dec % 2
dec = dec / 2
if remain > 0.5:
bin += '1'
else:
bin += '0'
return dec, bin
def printResult(binNumber):
print("The binary notation is:", binNumber[::-1]) # the last part is to reverse the string
menu()
如果我输入“2”,这就是结果
二进制表示法是:00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010