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 ...
随机推荐
- 根据identifier从StoryBoard中获取对象,UIButton的图片文件位置
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- /users/products.:format 这种写法的其对应解析字符写法
“products.:format" 这种写法可以有对应的下面两种路由形式 /products.json /products.xml "products.:format?" ...
- Autolayout-VFL语言添加约束-备
一.VFL语言简介 VFL(Visual format language)语言是苹果为了简化手写Autolayout代码所创建的专门负责编写约束的代码.为我们简化了许多代码量. 二.使用步骤 使用步骤 ...
- [TYVJ] P1017 冗余关系
冗余关系 背景 Background 太原成成中学第3次模拟赛 第4题 描述 Description Mrs.Chen是一个很认真很称职的语文老师 ......所以,当她看到学生作文里的人物关系描 ...
- 设置Ubuntu Mysql可以远程链接
1:修改my.cnf配置文件 $sudo vim /etc/mysql/my.cnf 修改为: bind-address = 0.0.0.0 2:进行授权操作 mysql> grant all ...
- The end of other
The end of other For language training our Robots want to learn about suffixes. In this task, you ar ...
- Uva 1342 - That Nice Euler Circuit
Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...
- 【HDU1233】还是畅通工程(MST基础题)
无坑,裸题.直接敲就恩那个AC. #include <iostream> #include <cstring> #include <cstdio> #include ...
- python高级编程之选择好名称:完2
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #分解代码 #小就是美,这也适用所有级别的代码,当一个函数,类或者一 ...
- [RxJS] Reactive Programming - Why choose RxJS?
RxJS is super when dealing with the dynamic value. Let's see an example which not using RxJS: var a ...