Python实现进制转换(华为机试)
题目描述
接收一个十六进制组成的字符串,输出该数值对应的十进制字符串
示例输入
0xA
示例输出
10
题目分析
题目的设计流程如下:
测试用例
1. 输入的数字字符串带有0x开头;
2. 输入的数字字符串带有0X开头;
3. 输入的数字字符串不是十六进制字符串
代码
def is_hex(hex):
for element in hex:
if (element.isdigit() == False) and (element != 'A') and (element != 'B') and \
(element != 'C') and (element != 'D') and (element != 'E') and \
(element != 'F') :
return False
else:
continue
return True
hex = input()
if hex[0:2] == '0x':
hex = hex[2:]
if is_hex(hex):
print(int(hex, 16))
else:
print('InputError: Illegal hex number inputs.')
传送门
1. input()函数
https://blog.****.net/TCatTime/article/details/82556033
2. int()函数