2014-01-29 1 views
0

좋아, 내가 LPTHW를 통해 가서 게임을 쓰고. 우리는 게임을 가져 오는 것이 어떻게되는지보기 위해 게임을 다른 스크립트로 구분해야합니다. 나는 4 개의 스크립트를 가지고있다. ex45.py은 주 코드를 실행하는데, Engine45.py은 엔진이고 Locations45.py은 위치 목록이고 Maps45.py은지도 값을 보유하고 위치를 이동하는 것이다.python NameError,하지만 그 이름이 정의 된 것 같아요

실행할 때 내가 오류는 다음과 같습니다 역 추적 (마지막으로 가장 최근 통화) : 파일 "ex45.py", 2 호선, 수입 Maps45 파일 "C에서 : \ 사용자 \ Raistlin \ 파이썬 \ Maps45. 파일 "C : \ Users \ Raistlin \ Python \ Locations45.py " 'Gold_gym'지도 라인 6 : Gold_gym(), 나가서 설명하자면 NameError : 이름이 'Gold_gym은'나는 골드를 얻고 이유를 이해하지 않습니다

을 정의되지 않은 _gym은 Locations45.py에 정의 된 경우 정의되지 않습니다. 아래

코드 블록 :

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 

Locations45.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) 

도와주세요.

답변

2

당신이 :

모듈 Locations45

그래서 네임 스페이스에서 가져온

import Locations45 

는 결코에서보고 알 수없는,

Gold_gym() 

그것은 그 객체에 대한 Maps45에 보이는 전화 Locations45.

변경 라인 읽기 : 당신은 그냥 import Locations45을 할 경우

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

굉장한이 시도를 줄 것이다, 나는 당신이 말할 수있는 것처럼 다소 새로운 ... – user3221870

+0

걱정 마라!우리 모두는 어딘가에서 시작합니다. – mhlester

1

, 그것은 이름의 각을 가져올 수 없습니다 - 대신, 그것은 그룹으로 가져옵니다. 당신은 기간 사용하여 그룹 내의 개별 사물을 참조 할 수 있습니다 : 또는

'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 

을 그 이름을 사용할 수 있도록 것이다 접두사를 사용하지 않고 .

+0

감사합니다! 도움에 감사드립니다. – user3221870

관련 문제