题目描述

接收一个十六进制组成的字符串,输出该数值对应的十进制字符串

示例输入

0xA

 

示例输出

10

 

题目分析

题目的设计流程如下:

Python实现进制转换(华为机试)

测试用例

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.csdn.net/TCatTime/article/details/82556033

2. int()函数

https://blog.csdn.net/TCatTime/article/details/82826824

相关文章:

  • 2022-12-23
  • 2021-11-02
  • 2021-11-26
  • 2021-08-03
  • 2021-11-17
  • 2021-11-13
  • 2021-12-04
  • 2022-12-23
猜你喜欢
  • 2021-12-01
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
  • 2023-01-06
相关资源
相似解决方案