Python教程

笨办法学python3 的习题43 调制版(解析版)

本文主要是介绍笨办法学python3 的习题43 调制版(解析版),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

       这个习题我搞了好久,模块这类感觉也挺难的,感觉还有好多没懂,看了好多好多回习题41、42。这道习题里面函数有很多调用,需要很多次打印,所以我就把注释,还有我的解题过程都放出来了。

 


from random import randint
from sys import exit
from textwrap import dedent


class Scene(object):
    print("______7_____class-Scene_____7_____\n")#因为没有调用函数就直接到下一个 Class了
    def enter(self):
        print(">>>>>>>>暂时不知道这里的是什么<<<<<<<<<<")
        print("This scene is not yet configured.")
        print("Subclass it and implement enter().")
        exit(1)


class Engine(object):
    print("_______16_____classEngine______16_____\n")
    def __init__(self, scene_map): # class Engine has-a __init__ that takes self and scene_map parameters
        self.scene_map = scene_map # Engine has-a attribute named scene_map
        print("_______19______ __init__  _______19______",scene_map)

    def play(self):
        print("_______22______play_______22_____")
        current_scene = self.scene_map.opening_scene()#在这里直接调用了 168的函数,所以就跳转到了167
        print("\n play of current_scene====24 24======" ,current_scene)#现在这里是CentralCorridor,由165 返还上来的
        last_scene = self.scene_map.next_scene('finished')
        print("last_scene======26 26=====" ,last_scene)#现在又将 第二次val的函数意义给return返回了这里,由165

        while current_scene != last_scene:
            print("\n^^^^^^ top of while current_scene==29====29== top ==", current_scene, "last_scene   ======== top =======",
            last_scene, )
            next_scene_name = current_scene.enter()#相当于 CentralCorridor.enter(),所以就开始去到了函数 CentralCorridor.  之后再该函数里面return的 地点名就是小写的地点名,并且是等于next——next_scene_name的
            print("next_scene_name == 32 ===== 32 ==",next_scene_name) #31行的函数结束之后就return 给了curren_scene一个新的值,并且从31继续进行下去
            current_scene = self.scene_map.next_scene(next_scene_name)#又一次165行return val了一个新的值  LaserWeaponArmory 给了curren_scene一个新的值。  即return的值是等于current_scene
            print("\n....... end of while current_scene === 34 === 34 ===   " ,current_scene, "last_scene========  ",
            last_scene,"next_scene_name ==== 35 ====  ", next_scene_name  )

        # be sure to print out the last scene
        current_scene.enter()


class Death(Scene):
    quips = [
        "You died. You kinda suck at this. ____________43____________",
        "Your mom would be proud...if she were smarter.__________44_______",
        "Such a luser.__________45____________",
        "I have a small puppy that's better at this.____________46________",
        "You're worse than yor Dad's jokes__________47____________"
    ]

    def enter(self):
        print(len(self.quips) - 1)
        print(Death.quips[randint(0, len(self.quips) - 1)])# 打印list里面的某一句话,似乎永远是只能是前四句,因为len只有5句,然后还减了个1
        exit(1)


class CentralCorridor(Scene):
    def enter(self):
        print(dedent("""\nWelcome to CentralCorridor  58 58___________"""))

        action = input("> ")

        if action == "shoot!":
            print(dedent(""" This is CentralCorridor CentralCorridor!!!  63 63_______________"""))
            return 'death'

        elif action == "dodge!":
            print(dedent(""" This is dodge! dodge! dodge!  67 67_______________________"""))
            return 'death'

        elif action == "tell a joke":
            print(dedent("""This is tell_a_joke  tell_a_joke tell_a_joke !!!   71 71____________________"""))
            return 'laser_weapon_armory'

        else:
            print("DOES NOT COMPUTE! 75 75__________________________")
            return 'central_corridor'


class LaserWeaponArmory(Scene):
    def enter(self):
        print(dedent("""\nLaserWeaponArmory  81_________________81"""))

        #code = f"{randint(1,9)}{randint(1,9)}{randint(1,9)}"
        code = "992"
        guess = input("[keypad]> 85___________85 ")
        guesses = 0

        while guess != code and guesses < 10:
            print("BZZZZEDDD!    89___________________89")
            guesses += 1
            guess = input("[keypad] 91________________91> ")

        if guess == code:
            print(dedent(""" This is LaserWeaponArmory LaserWeaponArmory!!! LaserWeaponArmory!!!  in if. 94________________94"""))
            return 'the_bridge'
        else:
            print(dedent("""This is LaserWeaponArmory LaserWeaponArmory also!! 97________________97"""))
            return 'death'


class TheBridge(Scene):
    def enter(self):
        print(dedent("""\nNow,you have come TheBridge TheBridge!!!  103________________-103"""))

        action = input(">>>105>>>> ")

        if action == "throw the bomb":
            print(dedent("""This is if in TheBridge   108________________________108"""))
            return 'death'

        elif action == "slowly place the bomb":
            print(dedent(""" This is elif in TheBridge  112_________________112"""))

            return 'escape_pod'
        else:
            print("DOES NOT COMPUTE! 116_____________________116")
            return 'the_bridge'


class EscapePod(Scene):
    def enter(self):
        print(dedent("""\nzhe shi zai taikongcang lim   122_______________________122"""))

        #good_pod = randint(1, 5)
        good_pod = 2
        guess = input("[pod #](125__________125)> ")

        if int(guess) != good_pod:
            print(dedent(f"""hui si d {guess} cang   128___________________128"""))
            print(f"第{good_pod}太空舱才是回家的路!!!")
            return 'death'
        else:
            print(dedent(f"""neng rang ren huo d {guess} cang   132________________________132"""))

            return 'finished'


class Finished(Scene):
    def enter(self):
        print("\nYou won! Good job.  139______________139")
        return 'finished'


class Map(object):
    print("_______144_____144______")
    scenes = {
        'central_corridor': CentralCorridor(),
        'laser_weapon_armory': LaserWeaponArmory(),
        'the_bridge': TheBridge(),
        'escape_pod': EscapePod(),
        'death': Death(),
        'finished': Finished()
    } #这里有一个字典,并且每次都调用了。  字典里面的函数需要在之前就进行定义

    def __init__(self, start_scene):
#定义函数 class map has-a __init__ that takes self and start_scene parameters.
        print("______156_______init_________156_____")
        self.start_scene = start_scene #因为这一行没有启动别的函数,所以就直接   from self get the start_scene attribute and set it to start_scene.
        print("==158===start_scene=158==",start_scene)# 这里 start_scene打印出来的是central_corridor 说明xx= aa

    def next_scene(self, scene_name):#来了这里两次,一次是从168带着corridor来到这样,一次是从25带着 finished来到这里
#定义函数 class map has-a function named next_scene that takes self and scene_name parameters
        print("______162______next_scene____162_______")
        val = Map.scenes.get(scene_name)#现在scene_name是start_scene,start_scene 又是每一次上面打开函数时的next_scene里面的值。  然后get的返回值是所导入键名的值
        print("====164=====v a l===164===", val)
        return val#使用类名得到字典里面的内容,然后回到play。
    print("--------------------166")
    def opening_scene(self):#  定义函数 opening——scene
        print("______168___________________168_____")
        return self.next_scene(self.start_scene)#将 self 和 start_scene 调给160的新函数next
    print("170-------------------/n")

a_map = Map('central_corridor')# 实际上 代码144那里上面的class都没有调用到函数,所以就一路到此,直接开始调用第一个Map函数
a_game = Engine(a_map)
a_game.play()

这篇关于笨办法学python3 的习题43 调制版(解析版)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!