这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!
原文例题
# -- coding: utf-8 --
from sys import exit
from random import randint
class Game(object):
# 类内初始化,其中新建初始值quips和start
def __init__(self, start):
self.quips = [
'You died.',
'Your mom would...',
'Such a luser',
'I have ...'
]
self.start = start
def play(self):
next = self.start
while True:
print "\n---------------------"
# 获取对象object的属性或者方法,如果存在打印出来(打印出来的是地址),如果不存在,打印出默认值,默认值可选。
room = getattr(self, next)
#指向这个地址的程序
next = room()
def death(self):
# 随机quips列表中的元素
print self.quips[randint(0, len(self.quips)-1)]
# 退出并关闭这段内存
exit(1)
def central_corridor(self):
print "The Gothons of..."
action = raw_input("> ")
if action == "shoot!":
print "Quick on the ..."
return 'death'
elif action == "dodge!":
print "Like a world ..."
return 'death'
elif action == "te;; a joke":
print "Lucky for you..."
return 'laser_weapon_armory'
else:
print "DOES NOT COMPUTE!"
return 'central_corridor'
def laser_weapon_armory(self):
print "You do a dive..."
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 "BZZZZEDDDD!"
guesses += 1
guess = raw_input("[keypad]> ")
if guess == code:
print "The container..."
return 'the_bridge'
else:
print "The lock..."
return 'death'
def the_brid