在这里我发现了return和print的区别
def binary_to_hex(binary_value):
len_text = len(binary_value)
for i in range(0, len_text, 4):
#能把二进制分成四组进行打印
print(binary_value[0 + i:4 + i])
#只能运行将前4个数分成一组
return binary_value[0 + i:4 + i]
a = binary_to_hex("1000100011001010")
print(a)
如果运行的是“print
”
如果运行"return"
解决
# 存储十六进制和二进制之间的代码
def binary_to_text(binary_text):
if binary_text == "0000":
return 0
elif binary_text == '0001':
return 1
elif binary_text == '0010':
return 2
elif binary_text == '0011':
return 3
elif binary_text == '0100':
return 4
elif binary_text == '0101':
return 5
elif binary_text == '0110':
return 6
elif binary_text == '0111':
return 7
elif binary_text == '1000':
return 8
elif binary_text == '1001':
return 9
elif binary_text == '1010':
return 'a'
elif binary_text == '1011':
return 'b'
elif binary_text == '1100':
return 'c'
elif binary_text == '1101':
return 'd'
elif binary_text == '1110':
return 'e'
elif binary_text == '1111':
return 'f'
def binary_to_hex(binary_value):
len_text = len(binary_value)
if len_text % 4 == 0:
for i in range(0, len_text, 4):
print(binary_to_text(binary_value[0 + i:4 + i]),end=" ")
else:
print("This is invalid number.")
def main():
binary_to_hex("1000100011001010")
main()