笨办法学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入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- 【算法入门】深度优先搜索(DFS)
深度优先搜索(DFS) [算法入门] 1.前言深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一个顶点V0开始,沿着一条路一直走到底,如果发现不能到达目标解 ...
- [CodePlus 2018 3 月赛] 博弈论与概率统计
link 题意简述 小 $A$ 与小 $B$ 在玩游戏,已知小 $A$ 赢 $n$ 局,小 $B$ 赢 $m$ 局,没有平局情况,且赢加一分,输减一分,而若只有 $0$ 分仍输不扣分. 已知小 $A$ ...
- HTML-美化
1.美化文本 1.1第一部分 font-size:字体大小,常用em.px.%.rem作单位,预设值small.large.medium,可继承, font-weight:加粗字体,属性为bold,加 ...
- DOM属性和事件
1-22 DOM属性设置与获取 1.获取属性: getAttribute("attribute"): var p = document.getElementById(" ...
- JS 的JSON对象
知识点一: 循环对象 for(x in Obj) x表示属性,Obj.x表示属性的值. 修改值 Obj.x = " "//直接修改 删除对象属性 delete Obj.x va ...
- JavaScript生成简单数字验证码
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- HTTPS原理以及流程
一.HTTP和HTTPS的区别 HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全. HTTPS协议是由SSL+HTTP协议构建的可进行加密传输.身份认证的网 ...
- 04-A的LU分解
一.矩阵$AB$的逆 $(AB)^{-1}=B^{-1}A^{-1}$,顺序正好相反 二.$A=LU$ 如矩阵: $\left[\begin{array}{ll}{2} & {1} \\ {8 ...
- python函数带不带括号的问题
Python带括号返回的是该函数的返回值 不带括号返回的是该函数的位置信息等
- 字符串与List互转
List转字符串,用逗号隔开 List<string> list = new List<string>(); list.Add("a"); list.Add ...