#encoding:GBK ‘‘‘ 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时 共有5个数相加),几个数相加由键盘控制。 1.程序分析:关键是计算出每一项的值。 2.程序源代码: ‘‘‘ a = int(input(‘please input a number which is between 1 and 9: ‘)) n = int(input(‘please input the numbers: ‘)) s = 0 if a < 1 or a > 10: a = int(input(‘please input a number which is between 1 and 9: ‘)) lista = [] for i in range(n): lista.append((i+1)*str(a)) print(lista) for i in range(n): s = s + int(lista[i]) print(s)
时间: 2024-10-27 08:09:54