下面是练习42,基于python3

 #ex42.py
1 class TheThing(object):
2 #__init__为class设置内部变量的方式,正常情况下函数内的变量与外部没有关联,但是这里的number变量却可以在class下关联
3 def __init__(self):
4 self.number = 5
5
6 def some_function(self):
7 print("I got called.")
8
9 def add_me_up(self, more):
10 self.number += more
11 return self.number
12
13 # two different things
14 a = TheThing()
15 b = TheThing()
16
17 a.some_function()
18 b.some_function()
19
20 print(a.add_me_up(20))
21 print(a.add_me_up(20))
22 print(b.add_me_up(30))
23 print(b.add_me_up(30))
24
25 print(a.number)
26 print(b.number)
27
28 # Study this. This is how you pass a variable
29 # from one class to another. You will need this.
30 class TheMultiplier(object):
31
32 def __init__(self, base):
33 self.base = base
34
35 def do_it(self, m):
36 return m * self.base
37
38 x = TheMultiplier(a.number)
39 print (x.do_it(b.number))
  #ex42+.py
1 #打印文档字符串 print(函数名.__doc__)
2 from sys import exit
3 from random import randint
4
5 class Game(object):
6
7
8 def __init__(self, start):
9 self.quips = ["You died. You kinda suck at this.",
10 "Nice job, you died ...jackass.",
11 "Such a luser.",
12 "I have a small puppy that's better at this."]
13 self.start = start
14
15 def play(self):
16 next = self.next
17
18 while True:
19 print("\n--------")
20 room = getattr(self, next)
21 next = room()
22
23 def death(self):
24 print(self.quips[randint(0, len(quips)-1)])
25 exit(1)
26
27
28 def central_corridor(self):
29
30 print("The Gothons of Planet Percal #25 have invaded your ship and destroyed")
31 print("your entire crew. You are the last surviving member and your last")
32 print("mission is to get the neutron destruct bomb from the Weapons Armory,")
33 print("put it in the bridge, and blow the ship up after getting into an ")
34 print("escape pod.")
35 print("\n")
36 print("You're running down the central corridor to the Weapons Armory when")
37 print("a Gothon jumps out, red scaly skin, dark grimy teeth,and evil clown costume")
38 print("flowing around his hate filled body. He's blocking the door to the")
39 print("Armory and about to pull a weapon to blast you.")
40
41 action = input("> ")
42
43 if action == "shoot!":
44 print("Quick on the draw you yank out your blaster and fire it at the Gothon.")
45 print("His clown costume is flowing and moving around his body, which throws")
46 print("off your aim. Your laser hits his costume but misses him entirely. This")
47 print("completely ruins his brand new costume his mother bought him, which")
48 print("makes him fly into an insane rage and blast you repeatedly in the face until")
49 print("you are dead, Then he eats you.")
50 return 'death'
51
52 elif action == "dodge!":
53 print("Like a world class boxer you dodge, weave, slip and slide right")
54 print("as the Gothon's blaster cranks a laser past your head.")
55 print("In the middle of your artful dodge your foot slips and you")
56 print("bang your head on the metal wall and pass out.")
57 print("You wake up shortly after only to die as the Gothon stomps on")
58 print("your head and eats you.")
59 return 'death'
60
61 elif action == "tell a joke":
62 print("Lucky for you they made you learn Gothon insults in the academy.")
63 print("You tell the one Gothon joke you know:")
64 print("Lbhe zbgure vf fb sng, jura fur fvgf nebhaq gur ubhfr, fur fvgf nebhaq gur ubhfr.")
65 print("The Gothon stops, tries not to laugh, then busts out laughing and can't move.")
66 print("While he's laughing you run up and shoot him square in the head")
67 print("putting him down, then jump through the Weapon Armory door.")
68 return 'laser_weapon_armory'
69 else:
70 print("DOES NOT COMPUTE!")
71 return 'central_corridor'
72
73 def laser_weapon_armory(self):
74 print("You do a dive roll into the Weapon Armory, crouch and scan the room")
75 print("for more Gothons that might be hiding. It's dead quiet, too quiet.")
76 print("You stand up and run to the far side of the room and find the")
77 print("neutron bomb in its container. There's a keypad lock on the box")
78 print("and you need the code to get the bomb out. If you get the code")
79 print("wrong 10 times then the lock closes forever and you can't")
80 print("get the bomb. The code is 3 digits.")
81 code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
82 guess = input("[keypad]> ")
83 guesses = 0
84
85 while guess != code and guesses < 10:
86 print("BZZZZEDDD!")
87 guesses += 1
88 guess = input("[keypad]> ")
89 if guess == code:
90 print("The container clicks open and the seal breaks, letting gas out.")
91 print("You grab the neutron bomb and run as fast as you can to the")
92 print("bridge where you must place it in the right spot.")
93 return 'the_bridge'
94 else:
95 print("The lock buzzes one last time and then you hear a sickening")
96 print("melting sound as the mechanism is fused together.")
97 print("You decide to sit there, and finally the Gothons blow up the")
98 print("ship from their ship and you die.")
99 return 'death'
100
101
102 def the_bridge(self):
103 print("You burst onto the Bridge with the neutron destruct bomb")
104 print("under your arm and surprise 5 Gothons who are trying to")
105 print("take control of the ship. Each of them has an even uglier")
106 print("clown costume than the last. They haven't pulled their")
107 print("weapons out yet, as they see the active bomb under your")
108 print("arm and don't want to set it off.")
109
110 action = input("> ")
111
112 if action == "throw the bomb":
113 print("In a panic you throw the bomb at the group of Gothons")
114 print("and make a leap for the door. Right as you drop it a")
115 print("Gothon shoots you right in the back killing you.")
116 print("As you die you see another Gothon frantically try to disarm")
117 print("the bomb. You die knowing they will probably blow up when")
118 print("it goes off.")
119 return 'death'
120
121 elif action == "slowly place the bomb":
122 print("You point your blaster at the bomb under your arm")
123 print("and the Gothons put their hands up and start to sweat.")
124 print("You inch backward to the door, open it, and then carefully")
125 print("place the bomb on the floor, pointing your blaster at it.")
126 print("You then jump back through the door, punch the close button")
127 print("and blast the lock so the Gothons can't get out.")
128 print("Now that the bomb is placed you run to the escape pod to")
129 print("get off this tin can.")
130 return 'escape_pod'
131
132 else:
133 print("DOES NOT COMPUTE!")
134 return "the_bridge"
135
136
137 def escape_pod(self):
138 print("You rush through the ship desperately trying to make it to")
139 print("the escape pod before the whole ship explodes. It seems like")
140 print("hardly any Gothons are on the ship, so your run is clear of")
141 print("interference. You get to the chamber with the escape pods, and")
142 print("now need to pick one to take. Some of them could be damaged")
143 print("but you don't have time to look. There's 5 pods, which one")
144 print("do you take?")
145
146 good_pod = randint(1,5)
147 guess = input("[pod #]> ")
148
149 if int(guess) != good_pod:
150 print("You jump into pod %s and hit the eject button." % guess)
151 print("The pod escapes out into the void of space, then")
152 print("implodes as the hull ruptures, crushing your body")
153 print("into jam jelly.")
154 return 'death'
155
156 else:
157 print("You jump into pod %s and hit the eject button." % guess)
158 print("The pod easily slides out into space heading to")
159 print("the planet below. As it flies to the planet, you look")
160 print("back and see your ship implode then explode like a")
161 print("bright star, taking out the Gothon ship at the same")
162 print("time. You won!")
163 exit(0)
164
165
166 a_game = Game("central_corridor")
167 a_game.play()
168
169
170

笨办法学Python(learn python the hard way)--练习程序42的更多相关文章

  1. 笨办法学 Python (Learn Python The Hard Way)

    最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...

  2. [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本

    黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...

  3. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  4. 笨办法学Python - 习题1: A Good First Program

    在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...

  5. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  6. 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘

    笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln  怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...

  7. 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍

    点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...

  8. 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程

    <笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...

  9. 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码

    笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...

  10. 《笨办法学Python 3》python入门书籍推荐|附下载方式

    <笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...

随机推荐

  1. ContextLoaderListener错误

    在web.xml中添加如下配置 <context-param> <param-name>contextConfigLocation</param-name> < ...

  2. 语言I—2019秋作业02

    这个作业属于那个课程 这个作业要求在哪里 我在这个课程的目标是 这个作业在那个具体方面帮助我实现目标 参考文献 C语言程序设计I https://edu.cnblogs.com/campus/zswx ...

  3. docker配置文件不生效

    1.查看docker配置文件位置 systemctl status docker.service 2.修改docker配置文件 vim /lib/systemd/system/docker.servi ...

  4. vue组件化编程应用

    写几个小案例来理解vue的组件化编程思想,下面是一个demo. 效果图示: 功能: Add组件用于添加用户评论,提交后右边评论回复会立马显示数据.Item组件点击删除可以删除当前用户评论.当List组 ...

  5. 洛谷 P1484 种树(优先队列,贪心,链表)

    传送门 解题思路 第一眼的贪心策略:每次都选最大的. 但是——不正确! 因为选了第i个树,第i-1和i-1棵树就不能选了.所以,要有一个反悔操作. 选了第i个后,我们就把a[i]的值更新为a[l[i] ...

  6. 在centos6.4下安装python3.5

    1.安装依赖包 ./configure --prefix=/usr/local/python3.5 --enable-shared make && make install yum g ...

  7. PyCharm中运行同一个python程序时选择平行窗口运行

    问题描述 当我们进行Socket编程时,客户端可能有多个,原则上如果有n个客户端,那么我们就要编辑n客户端的代码.然而其实我们每个客户端的代码都是相同,如果编辑n遍,将会相当的浪费空间. 解决办法 学 ...

  8. bootstrap使用总结(carousel设置大小。item设置大小,img设置大小)

    在bootstrap中使用carousel,先要给.carousel一个大小, 要想使carousel和item和img随着浏览器大小而变,就要设置 .carousel .item { height: ...

  9. 在django中使用循环与条件语言

    {% if not Article_type_id %} <li class="active"><a href="/">全部</a ...

  10. Excel批量插入的SQL Server

    首先新建一个WPF的项目,名为ExcelToServerDemo 到Nuget去安装NPOI的Nuget包,点击安装即可,会自动引用项目. 新建一个Student的表格,有名字,年龄,性别,地址,以及 ...