""" 输入字符串,判断只包含数字、a-z、A-Z、+-的数字串,进行+-,算出最小和 """ import re def sum_s(s): sum = 0 if re.match('^[0-9a-zA-Z+-]+$',s):#判断只包含数字、a-z、A-Z、+-的数字串 list1 = re.findall("[-0-9]+",s)#截取去掉+ for sl in list1: if "-" in sl:#有-时 if sl[0] != "-": x1 = sl.split("-",)#以-截取分开 print(x1) for x11 in x1[1:]:#从第二个开始计算负数和 if x11 != '':#去掉有空的 sum-=int(x11) sum = sum + int(x1[0])#首字符不是-,5--5,可以算 else: x2 = sl.split("-",)#首字符是-,-10h5,--5,可以算 print("x2==",x2) for x22 in x2: if x22 != '': sum -= int(x22) else: sum+= int(sl)#无-时,10h5,可以算 print(sum) else: print("输入了包含数字、a-z、A-Z、+-的字符") if __name__ == "__main__": s = input() sum = sum_s(s)
2021-1-2,笔记