首页 > 编程语言 > 详细

Python练习实例018

时间:2020-06-19 09:24:27      阅读:61      评论:0      收藏:0      [点我收藏+]

问题:求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

 

Python练习实例018

原文:https://www.cnblogs.com/mayi0312/p/13161221.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!