繁体   English   中英

python NameError,但我认为名称已定义

[英]python NameError, but I think the name is defined

好吧,我经历了LPTHW并编写了游戏。 我们应该将游戏分成不同的脚本,以了解如何导入它们。 我有4个脚本: ex45.py执行主代码, Engine45.py是引擎, Locations45.py是位置列表,而Maps45.py是保存地图值并在位置之间移动的Maps45.py

运行时出现的错误是:追溯(最近一次通话最近):导入Maps45中的文件“ ex45.py”,第2行,导入中的文件“ C:\\ Users \\ Raistlin \\ Python \\ Maps45.py”,第1行Locations45文件Map(object)中第4行的文件“ C:\\ Users \\ Raistlin \\ Python \\ Locations45.py”:Map'Gold_gym中第6行的文件“ C:\\ Users \\ Raistlin \\ Python \\ Locations45.py” ':Gold_gym(),NameError:名称“ Gold_gym”未定义

我不明白为什么在Locations45.py定义Gold_gym时未定义。

下面的代码块:

ex45.py

import Engine45
import Maps45
import Locations45


a_map = Map('Gold_gym')
a_game = Engine(a_map)
a_game.run()

Engine45.py

class Engine(object):

    def __init__(self, location_map):
        print "Engine __init__ has location_map", location_map
        self.location_map = location_map

    def run(self):
        current_location = self.location_map.opening_location()
        print "Play's first scene", current_location

        while True:
            print "\n"('-' * 20)
            next_location_name = current_location.use()
            print "next location", next_location_name
            current_location = self.location_map.next_location(next_location_name)
            print "map returns new location", current_location

位置45.py

from random import randint
from sys import exit


class Location(object):
    def use(self):
        print "This is not configured."
        exit(1)

class Loser_gym(Location):
    quips = [
        'You are worthless, goodbye.'
        'Thanks for trying noob.'
        'Sex and candy, not today.'
        ]

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

class Gold_gym(Location):
    def use(self):
        print "Welcome to the Gold Gym. This gym will test your physical prowess."
        print "Before you there is a large pool of water with a center platform."
        print "On your person is a satchel, a whip, a hat, and a gun."
        print "Your goal is to find the way out of the gym."
        print "Acceptable inputs are satchel, whip, hat, or gun."
        print "Good Luck."

        action = raw_input("What do you do:> ")

        if action == 'satchel':
            print "You throw your satchel towards the center platform."
            print "It does absolutely nothing. Try again."
            return 'Gold_gym'

        elif action == 'whip':
            print "You use your whip somewhat akin to Indiana Jones."
            print "You swing from a pipe and land squarely on the center platform."
            print "You have survived this turmoil."
            return 'Sapphire_gym'

        elif action == 'hat':
            print "You valiantly toss your hat toward the center platform."
            print "Your hat is caught by a gust of wind and blows away."
            print "You cannot survive without your hat."
            return 'Loser_gym'

        elif action == 'gun':
            print "You shoot your about wildly in the air."
            print "A ricochet hits you in the head and you die."
            return 'Loser_gym'

        else:
            print "DOES NOT COMPUTE, TRY AGAIN."
            return 'Gold_gym'

class Sapphire_gym(Location):
    def use(self):

        print "Welcome to the Sapphire gym, here your cognitive ability will be tested."
        print "Just kidding there is no way to figure this out consistently."
        print "Before you stands a door, the door has a small keypad on it."
        print "Below the keypad is what appears to be a smart card reader."
        print "On your way to the location you picked up two smartcards off the ground."
        print "One is red and one is green."
        print "Clearly you will need to use a smartcard and guess a code(3-digits)."

        card = raw_input('Which card do you choose:> ')

        if card == 'red':
            print "A laser beam comes out of the door and cauterizes your brain."
            return 'Loser_gym'

        elif card == 'green':
            code = '%d%d%d' % (randint(0,9), randint(0,9), randint(0,9))
            guess = raw_input('What code do you enter:> ')
            guesses = 0
            while guess != code and guesses < 20 and guess != '123':
                print "INCORRECT!"
                guesses += 1
                guess = raw_input('What code do you enter:> ')

            if guess == code or guess == '123':
                print "Nice guess noob! You may press onward."
                return 'Cerulean_gym'

            else:
                print "WRONG! TOO MANY GUESSES! DIE INFERIOR BEING!"
                return 'Loser_gym'


class Cerulean_gym(Location):
    def use(self):
        print "Welcome to the final gym, the Cerulean Gym!"
        print "Before you is a 3x3 platform, your job is to cross the platform."
        print "Be warned that each tile can trigger a trap."
        print "Good luck!"

        correct_square = ['3', '1', '2']
        jump = raw_input("Square 1, 2, or 3?:> ")
        jumps = 0

        while jumps < 3:
            if jump == correct_square:
                print "Nice job! You picked the correct square!"
                jump += 1
                correct_square = correct_square[0+jump]
                jump = raw_input("Square 1, 2, or 3?:> ")
                jumps += 1

            else:
                print "YOU FAIL! Goodbye you puny infidel."
                return 'Loser_gym'
        print 'NICE JOB. YOU WIN ALL THE BASE!'

Maps45.py

import Locations45


class Map(object):
    locations = {
        'Gold_gym' : Gold_gym(),
        'Sapphire_gym' : Sapphire_gym(),
        'Cerulean_gym' : Cerulean_gym(),
        'Loser_gym' : Loser_gym()
}

    def __init__(self, start_location):
        self.start_location = start_location
        print "start_location in __init__", self.start_location

    def next_location(self, location_name):
        print "start_location in next_location"
        val = Map.locations.get(location_name)
        print "next_location returns", val
        return val

    def opening_location(self):
        return self.next_location(self.start_location)

请帮助?

当你:

import Locations45

该模块是在命名空间Locations45下导入的

所以当你打电话

Gold_gym()

它在Maps45寻找该对象, Maps45知道在Locations45寻找。

将行更改为:

locations = {
        'Gold_gym' : Locations45.Gold_gym(),
        'Sapphire_gym' : Locations45.Sapphire_gym(),
        'Cerulean_gym' : Locations45.Cerulean_gym(),
        'Loser_gym' : Locations45.Loser_gym()

如果仅import Locations45 ,则不会导入每个名称-而是将其作为一个组导入。 您可以使用句点引用组中的单个事物:

'Gold_gym': Locations45.Gold_gym(),
'Sapphire_gym': Locations45.Sapphire_gym(),
'Cerulean_gym': Locations45.Cerulean_gym(),
'Loser_gym': Locations45.Loser_gym()

另外,您可以专门导入所有名称:

from Locations45 import Gold_gym, Sapphire_gym, Cerulean_gym, Loser_gym

这将使这些名称可用而无需使用前缀。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM