python 核心编程课后练习(chapter 2)
2-4
#2-4(a)
print "enter a string" inputstring = raw_input() print"the string is: ", inputstring
#2-4(b)
print "enter a number:" num = raw_input() print "the number is: ", int(num)
2-5
#2-5(a)
i = 0
while i<=10:
print i
i+=1
# 2-5(b)
for i in range(11)
print i
2-6
#2-6
print "please input a number:" num = raw_input() if int(num) < 0:
print" a negative number!!!"
elif int(num) > 0:
print "a positive number!!!"
else:
print "zero"
2-7
#2-7
print "please input a string:" inputstring = raw_input() while for e in inputstring:
print e
2-8
#2-8
print "please input a string:" inputstring = raw_input()
i = 0
while i<len(inputstring):
print inputstring[i],
i+=1
for e in inputstring:
print e
2-9
#2-9
arrary = [1, 2, 3, 4, 4] sum = 0
for e in arrary:
sum += e ev = float(sum)/float(len(arrary)) print ev
2-10
#2-10
print "please input a number between 1-100" num = int(raw_input()) while num<1 or num >100:
print "please input again:"
num = int(raw_input()) print "num is valid
2-11
#2-11
# to support cn
#encoding: utf-8
print """(1)取五個數的和"""
print "(2)取五個數的平均值..."
print "(x)退出"
def sum(arrary):
s = 0
for e in arrary:
s += e
return s def average(arrary):
if len(arrary) == 0:
return 0
else:
return float(sum(arrary))/float(len(arrary)) arr = [0, 1, 2, 3, 3] while True:
opt = raw_input()
if opt == 'x':
print "退出"
break
elif int(opt) == 1:
print "the sum of the arrary is :", sum(arr)
elif int(opt) == 2:
print "the average of the arrary is:", average(arr)
else:
print "please input again"
2-14
#2-14
#sort print "enter three number:" print "the first is:"
a = raw_input()
print "the second is:"
b = raw_input()
print "the third is:"
c = raw_input() print "after sort for low to high:" if a > b:
tmp = a
a = b
b = tmp
if a > c:
tmp = a
a = c
c = tmp if b > c:
tmp = b
b = c
c = tmp print a, b, c print "after sort for high to low:" if a < b:
tmp = a
a = b
b = tmp
if a < c:
tmp = a
a = c
c = tmp if b < c:
tmp = b
b = c
c = tmp print a, b, c
python 核心编程课后练习(chapter 2)的更多相关文章
- python 核心编程课后练习(chapter 6)
6-1 #6-1 #help(string) import string str = "helloworld" substr = "h1e" if string ...
- python 核心编程课后练习(chapter 5)
5-2 #5-2 def mul(x, y): return x * y print mul(4,5) 5-3 #5-3 def value_score(num): if 90<=num< ...
- python 核心编程课后练习(chapter 3)
3-8 #3-8 "makeTextFile.py -- create text file" import os ls = os.linesep #get filename fna ...
- Python核心编程课后习题-第六章
1. 字符串, string模块中是否有一种字符串方法或者函数可以帮我鉴定一下一个字符串是否是另一个大字符串的一部分? str1 = 'abcdefghijklmnopqrstuv' print st ...
- Python 核心编程 课后习题 第五章
2. 操作符. (a) 写一个函数, 计算并返回两个数的乘积. (b) 写一段代码调用这个函数, 并显示它的结果. def multi(a,b): return a * b result = mult ...
- Python核心编程 课后练习 第二章
2.4 使用raw_input()函数得到用户输入. (a) 创建一段脚本使用raw_input()函数从用户输入得到一个字符串, 然后显示这个用户杠杠输入的字符串. #coding = utf-8 ...
- python核心编程(第二版)习题
重新再看一遍python核心编程,把后面的习题都做一下.
- Python核心编程这本书的一些错误
<Python核心编程第二版>这本书比<Python基础教程第二版修订版>详细很多,丰富了很多细节,虽然它是一本经典的入门书,但我发现还是存在一些明显的错误.在面向对象编程这一 ...
- Python核心编程-描述符
python中,什么描述符.描述符就是实现了"__get__"."__set__"或"__delete__" 方法中至少一个的对象.什么是非 ...
随机推荐
- debug不过的程序
下面的程序debug是不能通过的. 至于为什么我还不知道. assume cs:codesg codesg segment start: mov ax,2000h mov ss, ax mov sp, ...
- 模仿console自写函数打印js的对象
本以为写个递归函数就可以将js的对象打印出来. 当然第一个想到的估计是JSON.stringify() 这个函数.但这个函数打印到浏览器 显示效果不友好.最友好的显示肯定是 控制台打印咯. 结果尝试打 ...
- centos 6 cglib
Error: Package: glibc-2.12-1.166.el6_7.3.i686 (@ultra-centos-6.7-updates) Requires: glibc-common = 2 ...
- lnmp搭建的常见错误
1:运行nginx时的错误 ./configure: error: the HTTP rewrite module requires the PCRE library. 解决: [root@svr11 ...
- 关于vue.js中class与style绑定的学习
练习代码: html: <!DOCTYPE html><html lang="en"><head> <meta charset=" ...
- MyEclipse黑色主题
第一步:打开链接http://www.eclipsecolorthemes.org/选中一款:下载其中的epf格式. 如图: 在eclipse中打开:file > import > Gen ...
- 如何用selenium webdriver 捕获js error
### 问题 捕捉页面上js error ### 解决办法 从Selenium webdriver log 中解析 # -*- coding:utf8 -*- import unittest from ...
- OpenGL观察轴
旋转矩阵可以通过观察向量构造,观察向量可以是3D空间的两个或三个点.如果一个处于P1点的对象面向P2点,则观察向量就是P2-P1,如下图: 首先,前轴向量通过归一化的观察向量简单计算而来. 其次,左轴 ...
- html框架
1.框架的概念 框架:将一个浏览器窗口划分成若干个小窗口 2.框架集合框架页 框架集<frameset>:主要用来划分窗口的. 框架页<frame>:主要用来指定窗口默认显示的 ...
- (转)Image Segmentation with Tensorflow using CNNs and Conditional Random Fields
Daniil's blog Machine Learning and Computer Vision artisan. About/ Blog/ Image Segmentation with Ten ...