python 核心编程第六章课后题自己做的答案
6–6. 字符串.创建一个 string.strip()的替代函数:接受一个字符串,去掉它前面和后面的 空格(如果使用 string.*strip()函数那本练习就没有意义了)
'Take a string and remove all leading and trailing whitespace' def newStrip(str):
'delete blanks around a string'
_end = len(str)
_start = 0 # delete the blanks at the beginning of the string
for i in range(_end):
if str[i] != ' ':
_start = i
break
else:
print 'invalid: The string is a blank string.' # if the string is a
_blank = True # blank string # delete the blanks in the end of the string
for i in range(-1, _start - _end, -1):
if str[i] != ' ':
_end = _end + i
break
if not _blank:
print '-' + str[_start: _end] + '-' # print '-' make sure the function work # test
if __name__ == '__main__':
newStrip(raw_input('Enter a string: '))
6–7. 调试.看一下在例 6.5 中给出的代码(buggy.py)
(a)研究这段代码并
python 核心编程第六章课后题自己做的答案的更多相关文章
- python核心编程第4章课后题答案(第二版75页)
4-1Python objects All Python objects have three attributes:type,ID,and value. All are readonly with ...
- python核心编程第5章课后题答案
5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the ...
- python核心编程第3章课后题答案(第二版55页)
3-4Statements Ues ; 3-5Statements Use\(unless part of a comma-separated sequence in which case \ is ...
- python核心编程第2章课后题答案(第二版36页)
2-5 Loops and Numbers a) i = 0 while i <11: print i i += 1 b) for i in range(0,11): pri ...
- python核心编程第六章练习6-8
6-8.列表.给出一个整型值,返回代表该值得英文,比如输入89会返回“eight-nine”.附加题:能够返回符合英文语法规律的新式,比如输入89会返回“eighty-nine”.本练习中的值假定在0 ...
- python核心编程第六章练习6-14
随机数.设计一个“石头.剪子.布”游戏,有时又叫“Rochambeau”,你小时候可能玩过,下面是规则.你和你的对手,在同一时间做出特定的手势,必须是下面一种:石头.剪子.布.胜利者从下面的规则产生, ...
- python核心编程第六章练习6-13
6-13.字符串.string模块包含三个函数,atoi(),atol()和atof(),他们分别负责把字符串转换成整型.长整型和浮点型数字.从Python 1.5起,Python的内建函数int() ...
- python核心编程第六章练习6-11
6-11.转换.(a)创建一个从整型到IP地址的转换,如下格式:www.xxx.yyy.zzz.(b)更新你的程序,使之可以逆转换.[答案](a)代码如下: Input_number = abs(in ...
- python核心编程第六章练习6-10
6-10.字符串.写一个函数,返回一个跟输入字符串相似的字符串,要求字符串的大小写反转,比如,输入“Mr.Ed”,应该返回“mR.eD”作为输出.[答案]代码如下: #!/usr/bin/env py ...
随机推荐
- 【7】python核心编程 第十一章-函数和函数式编程
1.*函数(与方法)装饰器 装饰器背后的主要动机源自python 面向对象编程.装饰器是在函数调用之上的修饰.这些修饰 仅是当声明一个函数或者方法的时候,才会应用的额外调用. 装饰器的语法以@开头,接 ...
- Choose the best route--hdu2680
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 计算textView的高度
- (CGFloat)measureHeightOfUITextView:(UITextView *)textView { if (floor(NSFoundationVersionNumber) & ...
- http、TCP/IP协议与socket之间的区别
http.TCP/IP协议与socket之间的区别 网络由下往上分为: www.2cto.com 物理层-- 数据链路层-- 网络层-- ...
- Android手机配置gcc,实现手机编译代码
1.下载gcc.zip 2.把gcc.zip解压存放在/data目录下(也可以是其他目录,看个人习惯) 3.配置gcc环境变量 export GCCHOME=/data/gcc (gcc存放路径) e ...
- ubuntu vim YCM
http://blog.sina.com.cn/s/blog_499386b00100rxm1.html http://www.cnblogs.com/junnyfeng/p/3633697.html
- sgu To xor or not to xor
题意:从n个数中,选择一些数,使得异或最大. #include <cstdio> #include <cstring> #include <algorithm> # ...
- UltraChart导出图片
? //一定要先绑定UltraChart,如果先绑定,然后有点击图片导出,没有用的 string fulPath="xxxx"; this.UltraChartTScore.Sav ...
- 让 SpringMVC 接收多个对象的4种方法
问题背景: 我要在一个表单里同时一次性提交多名乘客的个人信息到SpringMVC,前端HTML和SpringMVC Controller里该如何处理? 第1种方法:表单提交,以字段数组接收: 第2种方 ...
- 【转载】视频编码(H264概述)
一视频编码介绍 1.1 视频压缩编码的目标 1)保证压缩比例 2)保证恢复的质量 3)易实现,低成本,可靠性 1.2 压缩的出发点(可行性) 1)时间相关性 在一组视频序列中,相邻相邻两帧只有极少的不 ...