Python练习实例018

问题:求s=a+aa+aaa+aaaa+...+aaa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。

#! /usr/bin/env python3
# -*- coding:utf-8 -*-

# Author   : Ma Yi
# Blog     : http://www.cnblogs.com/mayi0312/
# Date     : 2020-06-19
# Name     : demo018
# Software : PyCharm
# Note     : 求s=a+aa+aaa+aaaa+...+aaa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几
# 个数相加由键盘控制。


# 入口函数
if __name__ == '__main__':
    num = input("Please input num:")
    times = int(input("Please input times:"))
    temp_list = []
    result = 0
    for i in range(times):
        temp_list.append(int(num * (i + 1)))
    for n in temp_list:
        result += n
    temp_list = [str(i) for i in temp_list]
    print("+".join(temp_list) + "=%d" % result)

运行结果:

Please input num:4
Please input times:7
4+44+444+4444+44444+444444+4444444=4938268

Please input num:2
Please input times:5
2+22+222+2222+22222=24690
原文地址:https://www.cnblogs.com/mayi0312/p/13161221.html