• 笨办法学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(" --------") 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(" ") 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
  • 相关阅读:
    windows相关命令记录
    使用addviewController()实现无业务逻辑跳转
    eclipse实用快捷键
    spring注解的相关配置
    day15-python-函数参数、名称空间、作用域
    day14-python-函数参数
    day13-seek、文件修改、函数及其参数
    day12-python-文件读取模式,文件指针移动
    day11-python-文件基础操作
    Ubuntu 更新软件的命令
  • 原文地址:https://www.cnblogs.com/xiyouzhi/p/9600560.html
Copyright © 2020-2023  润新知