GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python 练习实例18 | 菜鸟教程
Python 基础教程
Python 基础教程
Python 简介
Python 环境搭建
Python 中文编码
Python VS Code
Python 基础语法
Python 变量类型
Python 运算符
Python 条件语句
Python 循环语句
Python While 循环语句
Python for 循环语句
Python 循环嵌套
Python break 语句
Python continue 语句
Python pass 语句
Python Number(数字)
Python 字符串
Python 列表(List)
Python 元组
Python 字典(Dictionary)
Python 日期和时间
Python 函数
Python 模块
Python 文件I/O
Python File 方法
Python 异常处理
Python OS 文件/目录方法
Python 内置函数
Python 高级教程Python 面向对象 Python 正则表达式 Python CGI 编程 Python MySQL Python 网络编程 Python SMTP Python 多线程 Python XML 解析 Python GUI 编程(Tkinter) Python2.x 与 3.x 版本区别 Python IDE Python JSON Python AI 绘画 Python 100例 Python 测验Python 练习实例18题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。 程序分析:关键是计算出每一项的值。 程序源代码: 实例(Python 2.0+)#!/usr/bin/python
# -*- coding: UTF-8 -*-
Tn = 0
Sn = []
n = int(raw_input('n = '))
a = int(raw_input('a = '))
for count in range(n):
Tn = Tn + a
a = a * 10
Sn.append(Tn)
print Tn
Sn = reduce(lambda x,y : x + y,Sn)
print "计算和为:",Sn
实例(Python 3.0+)#!/usr/bin/python
from functools import reduce
Tn = 0
Sn = []
n = int(input('n = '))
a = int(input('a = '))
for count in range(n):
Tn = Tn + a
a = a * 10
Sn.append(Tn)
print (Tn)
Sn = reduce(lambda x,y : x + y,Sn)
print ("计算和为:",Sn)
以上实例输出结果为: n = 4 a = 4 4 44 444 4444 计算和为: 4936
AI 思考中...
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |