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