编写函数,计算a+aa+aaa+…+aa…aaaa,
的值,以及a-aa+aaa-…+(-1)n-1aa…aaaa的值,要求形参为a和n,并把a设为默认值参数2,同时返回两个结果。(a为1~9之间的数字)
def compu(num,a=2): #--------------------begin ---------------------------- sum1=0 sum2=0 for i in range(0,num): sum1+=a sum2+=(-1)**(i)*a a=a%10+a*10 return sum1,sum2 #---------------------end ------------------------------- if __name__=='__main__': sign=int(input()) if sign==0: num=int(input()) sum1,sum2=compu(num) if sign==1: num=int(input()) a=int(input()) sum1,sum2 = compu(num,a) print('sum1={0},sum2={1}'.format(sum1,sum2))