无类型对象属性错误Python

时间:2016-04-17 07:37:01

标签: python-2.7

在Zed Shaw的LPTHW之后在python 2.7中制作了一个文本游戏。它包括将不同的文件导入一个并调用它。游戏正在运行,但最后它给了我一个属性错误。

Traceback (most recent call last):
File "Main.py", line 15, in <module> a_game.play()
File "C:\mystuff\Escape\Game_Engine.py", line 17, in play next_scene_name = current_scene.enter() #enter the current scene calling 'enter' function.
AttributeError: 'NoneType' object has no attribute 'enter' 

我的代码: 1. Rooms.py

   #scenes module with all the scenes of the game and navigation.

    from sys import exit
    from random import randint


    #Abstract Base  class for scenes
    class Scene(object):
    #abstract class method enter to enter scenes.
        def enter(self):
            print "this scene not fully configured yet , implement enter()"
            exit(1)



    #Inheriting from the Scene class 
    class Death(Scene):
     #create a list of ways to mock when you die.   
        ways = ["You deserve to die if you are so dumb!",

        "Action without logic brings Death!",
        "Such a loser! you die!" ,
        "My grandma plays better than you!",
        "My pet monkey plays this game better!"
        ]



        #using the enter method from abstract class Scene    
        def enter(self):
            print Death.ways[randint(0,len(self.ways)-1)]
            exit(1)

#Opening scene  with decisions       
class Entrance(Scene):
    def enter(self):
        print "Welcome to your new mission ETHAN HUNT!"

        print "Your mission if you choose to accept it is to sneak in the Rogue AI unit - Matrix and steal the nuclear codes , "
        print "Then you have to place your bomb in the server room, "
        print "and make your way through the roof for the waiting chopper to pick you up."
        print "Save the world from nuclear destruction!"
        print "Caution: the AI master unit - The Brain is said to be most intelligent virtual entity in the world!"
        print "You have to defeat him in a math problem."
        print "Do you accept the mission Ethan?"
        print "Enter cool music! Ding ding ding ding ding dang ding dang..."
        print "Enter yes or no."       
        choice = raw_input("> ")

        if choice == "yes":
            print "Brilliant Ethan! The world depends on you!"
            print "Now you are outside the entrance: what will you do?"  
            print "You have 2 options: 1.sneak 2. shoot "            
            action = raw_input("> ")

            if action == "sneak":
                print "Well done! You are through the entrance on the way to control room."
                print "No one suspects you!"
                return 'control_room'

            elif action == "shoot":
                print "Was a dumb move! The guards overpower you and shoot you to bits!"
                return 'death'

            else: 
                print "Invalid input"
                return 'entrance' 


        elif choice == "no":
            print "Coward! We choose to terminate you instead!"
            return 'death'

        else:
            print "Invalid input! Does Not Compute!"
            return 'entrance'







#Next scene with decisions           
class Control_Room(Scene):
    def enter(self):
        print "Now you are in the control room , your chance to proceed undetected!"
        print "you find the control room guards , you inform them of some fire mishap outside!"
        print "they leave to check the emergency.You meanwhile disable the CCTVs."
        print "you sneak out. But the guards notice you."
        print "They raise an alarm and quiz you!"
        print "You have 2 options : 1.shoot 2.joke"
        print "what you gonna do?"

        action = raw_input("> ")

        if action == "shoot":
            print "you raise an alarm! dumbass move!"
            print "they easily call other guards and shoot you to death!"
            return 'death'

        elif action == "joke":
            print "you joke with the guards and tickle their funny bone."
            print "they dont suspect you no more"
            print "proceed on the mission!"
            return 'AI_vault'

        else:
            print "invalid input"
            return 'control_room'





#Next scene             
class AI_Vault(Scene):
    def enter(self):
        print "welcome to the home of the Brain!"
        print "Now you have to solve 3 math problems to get through and destruct me!"
        print "I cant handle anyone else being more brainy than me!"
        print "If you answer all 3 , The Brain would be forced to self destruct."
        print "Here is your first problem!"
        print "what is: 4x4+4x4+4-4x4 "

        action = raw_input("> ")
        if action == "20":
            print "The Brain is furious , you got it right!"
            print "I am sure you cant answer this one though , you miserable Human!"
            print "Now to the next!"
            print '''
Imagine you're on a game show, and you're given the choice of three doors:
Behind one door is a million dollars, 
and behind the other two, nothing. You pick door #1, 
and the host, who knows what's behind the doors, opens another door, say #3, 
and it has nothing behind it. He then says to you, 
"Do you want to stick with your choice or switch?
What will increase your probability to win? stick to first choice  or switch? '''

            action = raw_input("> ")
            if action == "switch":
                print "The Brain's muscles are red with rage! "
                print "If you get this one right , he will self destruct with all the insult!!!"


            else:
                print "Now you die!"
                return 'death'

            print "what is the answer for: 6 / 2(1+2) ?"

            action = raw_input("> ")

            if  action == "9":
                print "Correct! You are a Math genius , that I never thought I would meet!"
                print "The brain lets out a painful groan....and he self destructs!"
                print "You just destroyed the Brain! Good going!" 
                return 'server_room'

            else:
                print "Wrong! Now you die!"
                return 'death'
        else:
            print "Wrong! Now you die!"
            return 'death'








 #server scene with use of random module                    
class Server_Room(Scene):
    def enter(self):
        print "Now you are in the server room , retrieve the key nuclear codes"
        print "You locate the case containing the nuclear codes. "
        print "You have to guess the keycode to open the case containing nuclear codes"
        print "After you guess , plant the bomb and escape to the roof."
        print "You have 5 guesses to guess the 2 digit  keycode for the container"
        print "The digits can only be between 1 and 3. Goodluck!"
        code = "%d%d" % (randint(1,3),randint(1,3))
        guess = raw_input("## ")
        chances = 0

        while guess != code and chances < 4:
            print "BZZZEEDDDD! Wrong!"
            guess = raw_input("[keypad]## ")
            chances += 1

        if guess == code:
            print "the container clicks open and you retrieve the nuclear codes. awesome!"
            print "now you plant the bomb!"
            print "the bomb starts ticking and its time to escape!"
            print " you escape to the Roof where the chopper awaits."
            return 'roof'
        else:
            print "the lock buzzes and the codes in papyrus roll melt away."
            print "you despair and wait for the guards to discover you."
            print "they capture you and put you through a dog's death!"
            return 'death'






#Final scene with a question             
class Roof(Scene):
    def enter(self):
        print "You escape to the rooftop using the AC ducts in the server room. "
        print "You reach the rooftop."
        print "Theres benjy waiting in the chopper hovering above."
        print "Now there is another challenge: Benjy needs to know its really you, Ethan."
        print "So you have to answer a random Science question."
        print "If you solve it , he throws the rope to you  , if not he shoots you."
        print "Which Scientist discovered Oxygen Gas?"
        guess = raw_input("> ")
        chances = 0

        while guess!= "Priestley" and chances < 4:
            print "wrong!"
            guess = raw_input("> ")
            chances += 1

        if guess == "Priestley":
            print "correct! Hop in Ethan!"
            print "you escape with the rope he throws and escape from the building bad ass style!" 
            print "As you fly away the city skyline , you enjoy the fireworks of the building. "

            print "Mission accomplished!!!!! Well done Ethan! That was hard!"

            return 'finished'

        else:
            print "We know rogue agents when we see one! you infiltrating bastard! now you die!"
            print "Benjy shoots you and you die on rooftop!"
            return 'death' 
  1. Room_maps.py
  2. Maps模块,使用键值配对定义类的2个主要方法和其中所有场景的字典。

    import Rooms
    import Game_Engine
    
    #create class map with a dictionary for scene reference. 
    class Map(object):
        scenes = {
        'entrance' : Rooms.Entrance(),
        'control_room' : Rooms.Control_Room(),
        'AI_vault' : Rooms.AI_Vault(),
        'server_room' : Rooms.Server_Room(),
        'roof' : Rooms.Roof(),
        'death' : Rooms.Death()
        }
    
        #constructor with start_scene as argument.    
        def __init__(self,start_scene):
            self.start_scene = start_scene
    
    
        #function to retrieve scenes from dictionary         
        def next_scene(self,scene_name):
            val = Map.scenes.get(scene_name)
            return val
    
        #using the next_scene function to display opening scene        
        def opening_scene(self):
            return self.next_scene(self.start_scene)
    
    1. Game_Engine.py
    2. 游戏引擎模块,使用方法播放运行游戏,并使用地图方法从一个场景到另一个场景。

      class Engine(object):
      
      #constructor with scene_map as argument
          def __init__(self,scene_map):
              self.scene_map = scene_map
      
      
      #function to enter opening scene        
          def play(self):
              current_scene = self.scene_map.opening_scene()
      
              while True:
                  print "<<<<<<<<<< MI-Escape from Rogue AI >>>>>>>>>>>>>> "
                  next_scene_name = current_scene.enter() #enter the current scene calling 'enter' function.
                  current_scene = self.scene_map.next_scene(next_scene_name) #to enter the next scene calling map function 
      
      1. Main.py(我在powershell中调用的文件。)

        调用其他模块的主模块

        导入Room_maps 进口房间 导入Game_Engine

        Map,Engine创建的实例

        a_map = Room_maps.Map(&#39; entry&#39;)

        创建了Game Engine的实例。

        a_game = Game_Engine.Engine(a_map)

        从Engine调用play函数开始游戏。

        a_game.play()

      2. 为什么我会收到该错误?我该怎么办?

        谢谢。

1 个答案:

答案 0 :(得分:0)

没有一个叫'完成'的房间。当你到达终点时,它将无法找到它。轻松修复主循环。

将游戏更改为:

def play(self):
    current_scene = self.scene_map.opening_scene()

    while current_scene is not None:
        print "<<<<<<<<<< MI-Escape from Rogue AI >>>>>>>>>>>>>> "
        next_scene_name = current_scene.enter() #enter the current scene calling 'enter' function.
        current_scene = self.scene_map.next_scene(next_scene_name) #to enter the next scene calling map function