笨办法学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入门书,适合对计算机了解不多,没有学过编程,但对编程感兴趣的读者学习使用.这本书以习题的方式引导读 ...
随机推荐
- 第五周实验报告&学习总结
实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档: 实验内容 1.已知字符串:"this is a test of java".按要求执 ...
- 关于this与e.target区别以及data-*属性
1 this与event.target 在编写事件函数时可以传入一个event参数,even参数可以使用一个target属性如even.target用以调用,其作用是指向返回事件的目标节点(触发该事件 ...
- 快速查看php文档技巧
在php源码中看到注释中的相关链接后 Ctrl+鼠标,浏览器打开 将输入栏的“en”改为“zh”即可变为中文文档,其他语言类推
- Django使用Celery进行异步任务
Celery Celery是一个功能完备即插即用的异步任务队列系统.它适用于异步处理问题,当发送邮件.或者文件上传, 图像处理等等一些比较耗时的操作,我们可将其异步执行,这样用户不需要等待很久,提高用 ...
- [LeetCode] 30. 串联所有单词的子串
题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...
- 修改admin中App的名称与表的名称
修改APP的名称: # coding:utf-8 from django.apps import AppConfig import os default_app_config = 'repositor ...
- MySQl查询语句大全
综合使用 查询 目录: #----综合使用 书写顺序 select distinct * from '表名' where '限制条件' group by '分组依据' having '过滤条件' or ...
- 原生JS滚动条位置处理
// 滚动条位置 var scrollPosition = { // 位置 result: 0, // 监听位置 rememberPosition: function () { var type = ...
- vector auto
#include <iostream>#include <vector>#include <string>using namespace std;using std ...
- [编译原理]用BDD方式开发lisp解释器(编译器)|开发语言java|Groovy|Spock
lisp是一门简单又强大的语言,其语法极其简单: (+ 1 2 ) 上面的意思 是:+是方法或函数,1 ,2 是参数,fn=1+2,即对1,2进行相加求值,结果是:3 双括号用来提醒解释器开始和结束. ...