笨办法学Python(learn python the hard way)--练习程序42
下面是练习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的更多相关文章
- 笨办法学 Python (Learn Python The Hard Way)
最近在看:笨办法学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注 ...
- [IT学习]Learn Python the Hard Way (Using Python 3)笨办法学Python3版本
黑客余弦先生在知道创宇的知道创宇研发技能表v3.1中提到了入门Python的一本好书<Learn Python the Hard Way(英文版链接)>.其中的代码全部是2.7版本. 如果 ...
- 笨办法学 Python (第三版)(转载)
笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html 摘自https://learn-python ...
- 笨办法学Python - 习题1: A Good First Program
在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...
- 笨办法学python 13题:pycharm 运行
笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...
- 笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘
笨办法学python - 专业程序员的养成完整版PDF免费下载_百度云盘 提取码:xaln 怎样阅读本书 由于本书结构独特,你必须在学习时遵守几条规则 录入所有代码,禁止复制粘贴 一字不差地录入代码 ...
- 笨办法学Python 3|百度网盘免费下载|新手基础入门书籍
点击下方即可百度网盘免费提取 百度网盘免费下载:笨办法学Python 3 提取码:to27 内容简介: 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用. ...
- 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程
<笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...
- 笨办法学python 第四版 中文pdf高清版|网盘下载内附提取码
笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机了解不多,没有学过编程,但对编程感兴趣的朋友学习使用.这本书以习题的方式引导读者一步一步学习编 程,从简单的打印一 ...
- 《笨办法学Python 3》python入门书籍推荐|附下载方式
<笨办法学Python 3>python入门书籍免费下载 内容简介 本书是一本Python入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- 银河麒麟v4.0.2 安装gscloud的简单过程
1. 本来想用 tar包安装 redis 结果总是报错, 提示需要make test 但是我已经make test 了 所以还是使用 apt-get来安装. 2. 方式 apt-get update ...
- 【Luogu P2201】【JZOJ 3922】数列编辑器
题面: Description 小Z是一个爱好数学的小学生.最近,他在研究一些关于整数数列的性质. 为了方便他的研究,小Z希望实现一个叫做"Open Continuous Lines Pro ...
- 【SSL1786】麻将游戏
题目大意: 给出一个矩阵,查询其中两个点连通线段数 正文: 看这题好眼熟... 实质和这道题是一模一样的,只不过由一条询问升级到多条询问.
- [LeetCode] 154. 寻找旋转排序数组中的最小值 II
题目链接 : https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目描述: 假设按照升序排序的数组在预 ...
- 2019 Multi-University Training Contest 2 - 1011 - Keen On Everything But Triangle - 线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6601 首先要贪心地想,题目要最长的边长,那么要怎么构造呢?在一段连续的区间里面,一定是拿出最长的三根出来比,这样 ...
- “程序包com.sun.tools.javac.util不存在” 问题解决
最近工作中在编译打包项目的时候遇到了如标题所示的问题,报这个错误的类是 com.sun.tools.javac.util.Pair.问题很诡异,在Idea可以导入此类,项目启动运行也很正常,但就是在打 ...
- java复习(4)异常
1.Java异常的分类和类结构图 1.Throwable是整个java异常体系的超类,所有的异常类都派生自这个类,包含Error和Exception这两个直接的子类,概括了所有能被当做异常跑出来的东西 ...
- Netty入门搭建
什么是Netty Netty 是一个基于 JAVA NIO 类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性. 为什么选择netty而不是使用NIO 1.使用 ...
- ab压测
安装:yum install -y httpd-tools 验证:ab -V ab -help:-n requests 要执行请求总数,默认会执行一个请求 -c concurrency 一次执行多个请 ...
- 您的浏览器没有获得Java Virtual Machine(JVM)支持。可能由于没有安装JVM或者已安装但是没有启用。请安装JVM1.5或者以上版本,如果已安装则启用它。
您的浏览器没有获得Java Virtual Machine(JVM)支持.可能由于没有安装JVM或者已安装但是没有启用.请安装JVM1.5或者以上版本,如果已安装则启用它. https://www.j ...