数字求和小工具
def sum_num(a,b): #求和函数 falg=0 sum=0 try: # type(a) or type(b) !=int if a==b: #判断a等于b的情况 sum=a or b print('----1----') print(sum) elif a-b==2 or b-a==2: #判断a-b或者b-a等于2的情况 if a>b: #判断a>b if (b==0 or b==1) and a%2!=0: #判断b为0或者为1且判断a是否为奇数,奇数的中位数为(a+b)/2 a+=1 while falg<b: falg+=1 sum+=a+b c=int(a/2)+1 sum+=c print('----2.1.0----') print(sum) elif (b==0 or b==1)and a%2==0: #判断b为0或者为1且判断a是否为偶数,偶数则多循环一次 b=1 while falg<=int(((a-b)/2)): falg+=1 sum+=a+b print('----2.1.1----') print(sum) elif a%2==0: #判断b为0或者为1且判断a是否为偶数,偶数则多循环一次 while falg<=int(((a-b)/2)): falg+=1 sum+=a+b print('----2.1.2----') print(sum) elif ((a-b)/2)<1 : #判断a-b小于2 sum=(a+b)*(int((a/2))+1) print('----2.1.3----') print(sum) elif a%2!=0 and (((a-b)%2!=0) or ((a-b)%2==0)) : #判断b为0或者为1且判断a是否为奇数,奇数的中位数为(a+b)/2 while falg<int(((a-b)/2)): falg+=1 sum+=a+b # print(sum) c=int(a/2)+1 sum+=c print('----2.1.4----') print(sum) elif b>a: if (a==0 or a==1)and b%2==0: a=1 while falg<=int(((b-a)/2)): falg+=1 sum+=a+b print('----2.2.1----') print(sum) elif (a==0 or a==1) and b%2!=0: a=1 while falg<int(((b-a)/2)): falg+=1 sum+=a+b c=int(b/2)+1 sum+=c print('----2.2.2----') print(sum) elif b%2==0: while falg<int(((b-a)/2)): falg+=1 sum+=a+b # print(sum) c=(b+a)/2 print('----2.2.3----') sum+=c print(sum) elif ((b-a)/2)<1: sum=(a+b)*(int((b/2))+1) print('----2.2.4----') print(sum) elif b%2!=0 and (((a-b)%2!=0) or ((a-b)%2==0)): while falg<int(((b-a)/2)): falg+=1 sum+=a+b c=int(b/2)+1 sum+=c print('----2.2.5----') print(sum) elif a-b>2 or b-a>2: if a>b: #判断a大于b的情况 if (b==0 or b==1) and a%2==0: b=1 while falg<=int(((a-b)/2)): falg+=1 sum+=a+b print('----3.1.1----') print(sum) elif (b==0 or b==1)and a%2!=0: b=1 while falg<int(((a-b)/2)): falg+=1 sum+=a+b print(sum) c=int(a/2)+1 sum+=c print('----3.1.2----') print(sum) elif a%2==0: while falg<int(((a-b)/2)): falg+=1 sum+=a+b c=(a+b)/2 sum+=c print('----3.1.3----') print(sum) elif ((a-b)/2)<1 : sum=(a+b)*(int((a/2))+1) print('----3.1.4----') print(sum) elif a%2!=0 and (((a-b)%2!=0) or ((a-b)%2==0)): while falg<=int(((a-b)/2)): falg+=1 sum+=a+b # print(sum) # c=int(a/2)+1 # print(c) # sum+=c print('----3.1.5----') print(sum) elif b>a: #正常情况 if (a==0 or a==1) and b%2==0: a=1 while falg<=int(((b-a)/2)): falg+=1 sum+=a+b print('----3.2.1----') print(sum) elif (a==0 or a==1) and b%2!=0: a=1 while falg<int(((b-a)/2)): falg+=1 sum+=a+b c=int(b/2)+1 sum+=c print('----3.2.2----') print(sum) elif b%2==0 : while falg<int(((b-a)/2)): falg+=1 sum+=a+b c=(a+b)/2 sum+=c print('----3.2.3----') print(sum) elif ((b-a)/2)<1: sum=(a+b)*(int((b/2))+1) print('----3.2.4----') print(sum) elif b%2!=0 and (((a-b)%2!=0) or ((a-b)%2==0)): while falg<=int(((b-a)/2)): falg+=1 sum+=a+b # print(sum) print('----3.2.5----') print(sum) elif a-b<=1 or b-a<=1: print('----4.0.0----') sum=a+b print(sum) except Exception as e: print("您的输入错误:%s" %e) sum_num(1,365)
脑子不够用当然只能脚踏实地的做事情!