我尝试编写一个算法,可以将一个数字转换成二进制并打印出来,但不知何故程序在打印之前就停止了。
exponent = 4
binary = ""
def decToBin(userInput):
global exponent, binary
while (2**exponent) == 0:
if (2**exponent) <= userInput:
userInput = userInput - (2**exponent)
binary = binary + "1"
else:
binary = binary + "0"
exponent = exponent - 1
return binary
print(decToBin(16))