在学Python,在看《Python核心编程》的pdf,做了Chap2的题目,答案为DIY

# Filename: 2-11.py
# Author: ChrisZZ
mylist = [1, 2, 3, 4, 6]
sum = 0
for i in mylist:
sum = sum + i
average = sum * 1.0 / len(mylist)
while True:
print 'Here we have a list:', mylist
option = raw_input('Whatyou gonna know(sum/average/exit)?')
if option == 'sum':
print 'the sum of the list is', sum
elif option == 'average':
print 'the average of the list is', average
elif option == 'exit':
print 'Bye'
break
else:
print 'Wrong option.Repeat.' # Filename: 2-5.py
# Author: ChrisZZ
i = 0
while i <= 10:
print i
i = i + 1 for j in range(11):
print j # Filename: 2-15.py
# Author: ChrisZZ
print 'Please input 3 number.'
print 'I will sort them without using sort algo'
a = float(raw_input('the first number'))
b = float(raw_input('the second number'))
c = float(raw_input('the third number'))
res1 = (a - b) * (a - c)
mylist = []
if res1 < 0: # a is the middle
if b > c:
mylist = [b, a, c]
else:
mylist = [c, a, b]
else:
if a > b and b > c:
mylist = [a, b, c]
elif a > c and c > b:
mylist = [a, c, b]
elif a < b and b < c:
mylist = [c, b, a]
elif a < c and c < b:
mylist = [b, c, a] print mylist # Filename: 2-10.py
# Author: ChrisZZ
while True:
num = float(raw_input('Enter a number in range(1,100):'))
if num > 100 or num < 0:
print 'not a good number. repeat.'
else:
print 'nice number.bye'
break # Filename: 2-8.py
# Author: ChrisZZ
mylist = [1, 2, 3, 4, 5]
sumW = 0
i = 0
while i < len(mylist):
sumW = sumW + mylist[i]
i = i + 1
print sumW sumF = 0
for i in mylist:
sumF = sumF + i
print sumF # Filename: 2-7.py
# Author: ChrisZZ
s = raw_input('Enter a string:')
print 'while loop:'
i = len(s)
while i > 0:
print s[-i]
i = i - 1 print 'for loop:'
for ch in s:
print ch # Filename: 2-6.py
# Author: ChrisZZ
num = float(raw_input('Enter a number:'))
if num < .0:
print 'Negative number'
elif num > .0:
print 'Positive number'
else:
print 'Zero'

  PS:这些题目都是分开写的py脚本,自己写了另一个脚本把他们重定向到了一个叫做result.txt的文件中,习题在~/workspace/python/xiti/路径,处理的脚本在~/workspace/python/,具体如下:

# Filename: process.py
# Author: ChrisZZ
import os
prefix = "/home/chris/workspace/python/xiti/"
filenames = os.listdir(prefix)
out = open('result.txt', 'w')
for k, v in enumerate(filenames):
f = open(prefix + v, 'r')
out.write('# Filename: %s\n' % v)
out.write('# Author: ChrisZZ\n')
for eachLine in f:
out.write(eachLine)
f.close()
out.write('\n')
out.close()

Pyhton核心编程-Chap2习题-DIY的更多相关文章

  1. pyhton 核心编程 正则表达式习题

    方案一 import re #1. 识别下列字符串:“bat,” “bit,” “but,” “hat,” “hit,” 或 “hut” import re def test1(self): bt = ...

  2. Python核心编程课后习题-第六章

    1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...

  3. Python 核心编程 课后习题 第五章

    2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...

  4. python核心编程(第二版)习题

    重新再看一遍python核心编程,把后面的习题都做一下.

  5. Qt on Android 核心编程

    Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著   ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...

  6. windows核心编程 - 线程同步机制

    线程同步机制 常用的线程同步机制有很多种,主要分为用户模式和内核对象两类:其中 用户模式包括:原子操作.关键代码段 内核对象包括:时间内核对象(Event).等待定时器内核对象(WaitableTim ...

  7. windows核心编程---第九章 同步设备IO与异步设备IO之同步IO

    同步设备IO 所谓同步IO是指线程在发起IO请求后会被挂起,IO完成后继续执行. 异步IO是指:线程发起IO请求后并不会挂起而是继续执行.IO完毕后会得到设备的通知.而IO完成端口就是实现这种通知的很 ...

  8. windows核心编程---第八章 使用内核对象进行线程同步

    使用内核对象进行线程同步. 前面我们介绍了用户模式下线程同步的几种方式.在用户模式下进行线程同步的最大好处就是速度非常快.因此当需要使用线程同步时用户模式下的线程同步是首选. 但是用户模式下的线程同步 ...

  9. Python核心编程这本书的一些错误

    <Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...

随机推荐

  1. C陷阱与缺陷的个人知识点摘录

    编译过程的一点心得体会: .h文件其实只在预处理的过程用到,用来将类似#include <stdio.h>这样的行展开为具体内容. 那些标准库或者其他库中的函数,是在链接的过程中连接器把相 ...

  2. K8S调度之标签选择器

    Kubernetes 调度简介 除了让 kubernetes 集群调度器自动为 pod 资源选择某个节点(默认调度考虑的是资源足够,并且 load 尽量平均),有些情况我们希望能更多地控制 pod 应 ...

  3. Java poi读取,写入Excel2003

    Java poi读取,写入Excel2003 相关阅读:poi读写Excel2007:http://www.cnblogs.com/gavinYang/p/3576741.htmljxl读写excel ...

  4. Tomcat——Tomcat使用详解

    Tomcat简介 官网:http://tomcat.apache.org/ Tomcat GitHub 地址:https://github.com/apache/tomcat Tomcat是Apach ...

  5. COGS 栅格网络流

    750. 栅格网络流 http://www.cogs.pro/cogs/problem/problem.php?pid=750 ★★☆   输入文件:flowa.in   输出文件:flowa.out ...

  6. Nginx模块Lua-Nginx-Module学习笔记(一)Nginx Lua API 接口详解

    源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一.介绍 各种* _by_lua,* _by_lua_block和* _by_lua_file配置指令用 ...

  7. MongoDB - MongoDB CRUD Operations, Delete Documents

    Delete Methods MongoDB provides the following methods to delete documents of a collection: Method De ...

  8. 装好Linux后没有声音的看过来

    现代的Linux发行版对声卡的支持都应该没有问题.系统装好,声卡就应该正常工作. (尤其是ArchLinux,我觉得对硬件支持最跟得上时代步伐.) 可是我用mplayer播放mp3文件却没有声音,但也 ...

  9. Eclipse配置C++环境

    由于实在不想用(界面太丑,超级强迫症),前段时间JAVA一直用eclipse,感觉这个IDE非常友好,看上去很舒服,下载的时候发现有C++版本,于是折腾了一会儿,谷歌上发现好多教程,但是大部分比较老, ...

  10. pentaho bi server 配置MySQL数据库

    软件版本: jdk 1.7 MySQL 5.5 biserver-ce-6.1.0.1-196 (选择右下方的所有选项See All Activities) 一.前置环境安装 1.安装jdk(略) 2 ...