1. 程式人生 > >笨辦法學python 習題42 加分練習

笨辦法學python 習題42 加分練習

art 學python pup row die 另一個 ddd you true

3、創建一個新版本,裏邊使用兩個 class,其中一個是 Map ,另一個是 Engine 。提示: 把 play 放到 Engine 裏面。、

#coding=utf-8
from sys import exit
from random import randint

class Map(object):
def __init__(self):
self.quips = ["You died. You kinda suck at this.","Your mom would be proud. If she were smarter.","Such a luser.","I hava a small puppy that‘s better at this."]

def death(self):
print self.quips[randint(0,len(self.quips)-1)]
exit(1)


def central_corridor(self):
action = raw_input(">>> ")
if action == "shoot!":
return ‘death‘
elif action == "dodge!":
return ‘death‘
elif action == "tell a joke":
return ‘laser_weapon_armory‘
else:
return ‘central_corridor‘

def laser_weapon_armory(self):
code = "%d%d%d"% (randint(1,9)),(randint(1,9)),(randint(1,9))
guess = raw_input("[keypad]>>> ")
guesses = 0
while guess !=code and guesses < 10:
print "BZZZZEDDD!"
guesses += 1
guesses = raw_input("[keypad]>>> ")
if guess == code:
return ‘the_bridge‘
else:
return ‘death‘

def the_bridge(self):
action = raw_input(">>> ")
if action == "throw the bomb":
return ‘death‘
elif action == "slowly place the bomb":
return ‘escape_pod‘
else:
return ‘the_bridge‘

def escape_pod(self):
good_pod = randint(1,5)
guess = raw_input("[pod #]>>> ")
if int(guess) != good_pod:
return ‘death‘
else:
exit(0)

class Engine(Map):
def __init__(self,start):
self.start = start

def play(self):
next = self.start
while True:
print "\n-----------"
room = getattr(self,next)
next = room()

a_game = Engine("central_corridor")
a_game.play() 備註:初學小菜鳥,歡迎各位大神指教

笨辦法學python 習題42 加分練習