直接上代码:
def str_to_num(num_str):
"""
将字符串转化为整数
:param num_str: 字符串数字
:return: 整型数字
"""
number_dict = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
num_str_turn = num_str[:: -1]
n = 0
add_sum = 0
for num in num_str_turn:
for key in number_dict.keys():
if key == num:
add_sum = add_sum + number_dict[key] * 10 ** n
n = n + 1
return add_sum
if __name__ == '__main__':
num_str = input("Input number:")
if num_str.isdigit():
num_int = str_to_num(num_str)
print(num_int)
else:
print("Input error!")