《笨办法学python》加分习题42——我的答案

本文分享了作者在学习《笨办法学Python》时的加分习题答案,包括原文例题的个人注释和额外的习题解答,期待读者指正交流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是我自己学习的答案,会尽力写的比较好。还望大家能够提出我的不足和错误,谢谢!

原文例题

# -- 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_bridge(self):
        print "You burst onto..."
        action = raw_input("> ")

        if action == "throw the bomb":
            print "In a panic..."
            return 'death'

        elif action == "slowly place the bomb":
            print "You point your..."
            return 'escape_pod'
        else:
            print "DOES NOT COMPUTE!"
            return "the_bridge"

    def escape_pod(self):
        print "You rush through..."

        good_pod = randint(1,5)
        guess = raw_input("[pod #]> ")


        if int(guess) != good_pod:
            print "You jump into..."
            return 'death'
        else:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod..."
            exit(0)

# 构造函数,调用_init_函数
a_game = Game("central_corridor")
# 通俗的讲,上一句声明了Game a_game("central_corridor")(别纠结语法,)我就图个好了解
# 调用play的类内函数
a_game.play()

以上附上个人注释,如有不当,望指出。

加分习题

2、

# -- 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 == "take a joke":
            print "Lucky for you..."
            return 'laser_weapon_armory'
        # 添加内容
        elif action == "riddle":
            print "Let's go!"
            return 'riddles'
        # *********************

        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_bridge(self):
        print "You burst onto..."
        action = raw_input("> ")

        if action == "throw the bomb":
            print "In a panic..."
            return 'death'

        elif action == "slowly place the bomb":
            print "You point your..."
            return 'escape_pod'
        else:
            print "DOES NOT COMPUTE!"
            return "the_bridge"

    def escape_pod(self):
        print "You rush through..."

        good_pod = randint(1,5)
        guess = raw_input("[pod #]> ")


        if int(guess) != good_pod:
            print "You jump into..."
            return 'death'
        else:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod..."
            exit(0)
    # 添加内容
    def riddles(self):
        print "What will you break once you say it?"
        answer = raw_input(" ")

        if answer == "Silence":
            print "Lucky for you!"
            return 'succeed'
        else:
            print "Gone"
            return 'death'
    def succeed(self):
        print "You are lucky boy."
        print '>'*10
        print "Succeed"
        exit(0)
# 构造函数,调用_init_函数
a_game = Game("central_corridor")
# 通俗的讲,上一句声明了Game a_game("central_corridor")(别纠结语法,我就图个好了解)
# 调用play的类内函数
a_game.play()

添加了两个类内函数succeed(self)和riddles(self)。

3、

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

class Map(object):
    def __init__(self):
        self.quips = [
             "You died. You kiinda suck at this.",
             "Nice job, you died ...jackass.",
             "Such a luser.",
             "I hace 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):
        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 == "take a joke":
            print "Lucky for you..."
            return 'laser_weapon_armory'

        elif action == "riddle":
            print "Let's go!"
            return 'riddles'

        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_bridge(self):
        print "You burst onto..."
        action = raw_input("> ")

        if action == "throw the bomb":
            print "In a panic..."
            return 'death'

        elif action == "slowly place the bomb":
            print "You point your..."
            return 'escape_pod'
        else:
            print "DOES NOT COMPUTE!"
            return "the_bridge"

    def escape_pod(self):
        print "You rush through..."

        good_pod = randint(1,5)
        guess = raw_input("[pod #]> ")


        if int(guess) != good_pod:
            print "You jump into..."
            return 'death'
        else:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod..."
            exit(0)

    def riddles(self):
        print "What will you break once you say it?"
        answer = raw_input(" ")

        if answer == "Silence":
            print "Lucky for you!"
            return 'succeed'
        else:
            print "Gone"
            return 'death'

    def succeed(self):
        print "You are lucky boy."
        print '>'*10
        print "Succeed"
        exit(0)


class Engine(object):

    def __init__(self, game):

        self.game = game


    def play(self):

        next = self.game

        while True:
            print "\n---------------------"
            room = getattr(Map(), next)
            next = room()

a_game = Engine("central_corridor")

a_game.play()

好长啊,不过很多是重复的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值