Learn Python the Hard Way
chaoqichaoluoe
Transform!
展开
-
ex38——列表的操作
ten_things = "Apples Oranges Crows Telephone Light Sugar"print("Wait there's not 10 things in that list, let's fix that.")# 将字符串按空格分开为列表str.split(' ')stuff = ten_things.split(' ')more_stuff = ["Da...原创 2019-08-23 19:48:12 · 117 阅读 · 0 评论 -
ex25——对句子进行打断,弹出单词,排序
def break_words(stuff): """This function will break up words for us.""" # stuff.split('a'):字符串stuff以a隔断,此处以空格隔断 words = stuff.split(' ') return wordsdef sort_words(words): """So...原创 2019-08-22 21:00:25 · 105 阅读 · 0 评论 -
ex26——modify code
def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return wordsdef sort_words(words): """Sorts the words.""" return sorted(words)...原创 2019-08-22 21:53:39 · 120 阅读 · 0 评论 -
ex35——分支和函数
from sys import exitdef gold_room(): print("This room is full of gold. How much do you take?") next = input(">") if "0" in next or "1" in next: how_much = int(next) else...原创 2019-08-23 12:56:30 · 157 阅读 · 0 评论 -
ex43——基本的面向对象分析和设计
难点在与类Map和Engine的相互调用:在Map中先建立字典dict{场景名称:场景类名},Engine中建立循环,通过实例的返回值(场景名称),建立起与场景类名的相关性,通过场景名称调用将相关类名实例化。Map中的current、opening、next方法与循环结合后的逻辑性,推动这个文字游戏不断运行。此例很好的演示了python面向对象分析和设计的方法。dict.get(ke...原创 2019-08-27 22:13:45 · 327 阅读 · 0 评论 -
ex44——继承与合成
class Parent(): def override(self): print("parent override()") def implicit(self): print("parent implicit()") def altered(self): print("parent altered()")class...原创 2019-08-28 20:44:14 · 131 阅读 · 0 评论 -
ex47——自动化测试nose和assert_equal
被测试的代码class Room(): def __init__(self, name, description): self.name = name self.description = description self.paths = {} def go(self, direction): # dict.get...原创 2019-08-29 08:08:53 · 569 阅读 · 0 评论 -
ex48——更复杂的用户输入/扫描输入
lexicon程序def convert_number(s): """处理异常和数字的函数""" try: return int(s) except ValueError: return Noneclass Lexicon(): # 因主程序中调用的是lexicon.scan(),因此只能是由定义类的属性延伸到函数的属性 ...原创 2019-09-01 15:29:17 · 174 阅读 · 0 评论 -
ex46和ex48——项目骨架和更复杂的用户输入
**projects/setup.py**try: from setuptools import setupexcept ImportError: from distutils.core import setupconfig = { 'description': 'My project', 'author': 'My name', 'url': ...原创 2019-09-05 22:02:34 · 291 阅读 · 0 评论 -
ex24——单引号(')、双引号(")、三引号(''',""")
print("Let's practice everything.")# \'→ ',\\ → \, \n→ 换行, \t→空格print("You\'d need to know \'about escapes with \\ that do \n newlines and \t tabs.")# 直接打印多行内容poem = """\tThe lovely worldwith lo...原创 2019-08-22 17:19:38 · 405 阅读 · 0 评论 -
ex20——python文件读read()、readline()、readlines()对比
“”"Open的current_file文件只是一个变量而已,不过在这里它指的是一个文件,python里的文件有一个读取数据的“磁头”,通过“磁头”操作文件,运行f.seek(x)就回到了文件的第x个字符,运行f.readline()则会读取文件的“磁头”至下一个|n为至的代码。filename.read():读取整个文件,直接读取字节到字符串中,包括了换行符filename.read...原创 2019-08-22 14:35:59 · 200 阅读 · 0 评论 -
ex29——if语句
people = 20cats = 30dogs = 15if people < cats: print("Too many cats! The world is doomed!")if people > cats: print("Not many cats! The world is saved!")if people < dogs: p...原创 2019-08-23 08:15:33 · 100 阅读 · 0 评论 -
ex32——循环和列表
the_count = [1, 2, 3, 4, 5]fruits = ['apples', 'oranges', 'pears', 'apricots']change = [1, 'pennies', 2, 'dimes', 3, 'quarters']# 遍历列表the_count(int)的for循环,可以使用%d和%s和%rfor number in the_count: ...原创 2019-08-23 09:08:24 · 137 阅读 · 0 评论 -
ex30——else和if
people = 30cars = 40buses = 15if cars > people: print("We should take the cars")elif cars < people: print("We should't take the cars")else: print("We can't decide.")if buses ...原创 2019-08-23 08:24:17 · 94 阅读 · 0 评论 -
ex31——if、else、elif
print("You enter a dark room with two doors. Do you go through door #1 or door #2?")door = input(">")if door == "1": print("There's a giant bear here eating cheese cake. What do you do?") ...原创 2019-08-23 08:47:30 · 93 阅读 · 0 评论 -
ex33——while循环
while循环会经常出现无法结束的情况,为避免类似问题,需遵循以下规定:优先使用for循环,而不是while循环重复检查while语句,确定测试的布尔表达式最终会变成False若不确定,就在while循环结尾打印出要测试的值,监视其变化。i = 0numbers = []while i < 6: print("At the top i is %d" % i) ...原创 2019-08-23 09:20:12 · 142 阅读 · 0 评论 -
ex35——字典
# 创建省到缩写的字典provinces = { 'Shandong': "SD", 'Henan': 'HN', 'Gansu': 'GS', 'Jiangsu': 'JS', 'Shanxi': 'SX'}# 创建省缩写到主要城市到字典cities = { 'SD': 'Qingdao', 'HN': 'Zhengzhou',...原创 2019-08-24 21:10:36 · 93 阅读 · 0 评论 -
ex41——学习面向对象术语
import randomfrom urllib.request import urlopenimport sys# 得到外部网址文件地址,并赋值给WORD_URLWORD_URL = "http://learncodethehardway.org/words.txt"# 定义空列表WORDWORDS = []'''定义字典PHRASES,其内容是解释类的格式:class %%%...原创 2019-08-25 14:16:51 · 239 阅读 · 0 评论 -
ex21——return使用方法
def add(a, b): print("Adding %d + %d" % (a, b)) return a + bdef subtract(a, b): print("Subtracting %d - %d" % (a, b)) return a - bdef multiply(a, b): print("Multiplying %d * ...原创 2019-08-22 15:20:34 · 664 阅读 · 0 评论 -
ex49——创建句子
仍然使用ex46的项目骨架进行测试,NAME文件夹命名为ex49,其中放置需要测试的模块parser.py,也可以使用ex48中的NAME文件,将两次或多次测试一并进行。tests文件夹中放置parser_test.py.from NAME import module #NAME文件夹名,module模块名**parse_tests.py**from nose.tools import *...原创 2019-09-06 08:08:35 · 277 阅读 · 0 评论