为什么我总是在python中遇到这个大错误。回溯(最近一次呼叫。。。。。和归属者

2024-04-19 02:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在学习python,从互联网上获取不同的东西,并把它们都放进我正在制作的这个游戏中:“你醒了……”这是一个基于文本的assci-RPG(滚动游戏)。直到我犯了这个错误:

Traceback (most recent call last):
  File "C:\Users\William\Desktop\Programming\Programs\You wake up\main.py", line 16, in <module>
    import helper
  File "C:\Users\William\Desktop\Programming\Programs\You wake up\helper.py", line 13, in <module>
    import main
  File "C:\Users\William\Desktop\Programming\Programs\You wake up\main.py", line 103, in <module>
    init()
  File "C:\Users\William\Desktop\Programming\Programs\You wake up\main.py", line 41, in init
    game_text()
  File "C:\Users\William\Desktop\Programming\Programs\You wake up\main.py", line 77, in game_text
    helper.way_out_quest()
AttributeError: 'module' object has no attribute 'way_out_quest'

这是主文件(main.py):

#-------------------------------------------------------------------------------
# Name:        main.py
# Purpose:     An RPG (Roll playing game) where you wake up in a room and have
#              to figure out text based puzzles to escape.
#              The whole game will be done in a terminal (Shell) and will be
#              completely text and assci code. This will be the main file.
#
# Author:      William
#
# Created:     15/12/2013
# Copyright:   (c) William 2013
#-------------------------------------------------------------------------------

import time
import assci
import helper

#Non-maluable variables:
__name__ = '__main__'
#Maluable variables:

#Error message if main.py is called from a different file
if __name__ != '__main__':
    print("[ERROR]: main.py is the main file and should not be called by any other file.")
    time.sleep(3)
    exit()

#The function that starts/restarts the game
def init():
    """
    Calls all the functions to start the game/ restart the game
    """
    #Display a cool banner
    assci.ywu_banner(2)

    START_INPUT = input("Press ENTER/RETURN on your keyboard to start the game")
    time.sleep(0.7)
    assci.clear()

    #Game text.
    game_text()

#The text which is the main story line after the banner which gives the player
#A sense of what the setting is about
def game_text():
    """
    Prints out a bit of text 2 lines down and clears the screen every 4 or so
    seconds.
    """
    time.sleep(5)

    print("\n\nYour eyes gradually open")
    time.sleep(4)
    assci.clear()
    time.sleep(2)

    print("\n\nAfter waking up, you slowly gather your senses and sit up...")
    time.sleep(4.5)
    assci.clear()
    time.sleep(2)

    print("\n\nYou look around the room, and unexpectidly, you realise your in some sort of a prison cell!")
    time.sleep(4)
    assci.clear()
    time.sleep(2)

    print("\n\nIt's a sqaure room made of iron.")
    time.sleep(4)
    assci.clear()
    time.sleep(2)

    print("\n\nHow did I get here? -You think to yourself")
    time.sleep(4)
    assci.clear()
    time.sleep(2)

    helper.way_out_quest()

    assci.clear()
    time.sleep(2)

    print("\n\nYou see a wooden door with 5 buttons and a handel...  " + assci.WOODEN_DOOR)
    time.sleep(6)
    assci.clear()
    time.sleep(2)

    print("\n\nWhat was that? ...you think to your self.")
    time.sleep(4)
    assci.clear()
    time.sleep(2)

    print("\n\nWhat was that? ...you think to your self.")
    time.sleep(4)
    assci.clear()
    time.sleep(2)

    print("\n\nYou look around the room and walk slowly towards the iron door..")
    time.sleep(4)
    assci.clear()
    time.sleep(2)

#Call init() function to start the game (init = initiate)
init()

助手.py:

#-------------------------------------------------------------------------------
# Name:        helper
# Purpose:     To contain helper functions for the game "Yoy wake up..."
#
# Author:      William
#
# Created:     17/12/2013
# Copyright:   (c) William 2013
#-------------------------------------------------------------------------------

import time
import assci
import main

#Error message if user executes the wrong file.
if __name__ == '__main__':
    print("[ERROR]: Do not run this file. Run main.py - this file should not be executed!")
    time.sleep(4)
    exit()

#Function for the first quest: do you want to find a way out?
def way_out_quest():
    """
    If the question is not answered, then the player can't move on. If they say
    yes, then they continue through the script. If they say no, then the init
    function is called from main.py
    """
    way_out_answered = False
    while way_out_answered == False:
        WAY_OUT_INPUT = input("Quest: Do you want to find a way out? ")
        if WAY_OUT_INPUT in ["yes", "Yes", "YES"]:
            way_out_answered = True

        elif WAY_OUT_INPUT in ["no", "No", "NO"]:
            way_out_answered = True
            time.sleep(2)
            assci.clear()
            print("GAME\nOVER!")
            time.sleep (5)
            assci.clear()
            main.init()

        else:
            print("Type yes or no. ")

        time.sleep(4)

这是assci文件,包含所有的art/clear和text函数和内容(assci.py):

#-------------------------------------------------------------------------------
# Name:        assci
# Purpose:     To create all the assci art and graphic tools for the game:
#              "you wake up..."
#
# Author:      William Bryant
#
# Created:     15/12/2013
# Copyright:   (c) William 2013
#-------------------------------------------------------------------------------

import time
import os

WOODEN_DOOR = """ ______________________________
/_____/_____/_____/_____/_____/
 ._.                    ._.
 | |                    | |
 |_|   ______   ______  |_|
 |-|  /_____/  /_____/  |-|
 | |                    | |
 |_|                    |_|
 ._.                    ._.
 | |                    | |
 |_|   ______           |_|
 |-|  /_____/           |-|
 | |              /\    | |
 |_|              \/    |_|
 ._.                    ._.
 | |                    | |
 |_|   ______   ______  |_|
 |-|  /_____/  /_____/  |-|
 | |                    | |
 |_|                    |_|

  ______   ______   ______   ______   ______   ______   ______   ______
 /_____/  /_____/  /_____/  /_____/  /_____/  /_____/  /_____/  /_____/
 /_____/  /_____/  /_____/  /_____/  /_____/  /_____/  /_____/  /_____/"""

#Error message if user executes the wrong file.
if __name__ == '__main__':
    print("[ERROR]: Do not run this file. Run main.py - this file should not be executed!")
    time.sleep(4)
    exit()

#Clear function
def clear():
    """
    Clears the console screen using the built in commands on a operating
    system (here linux and windows)
    """
    os.system(['clear','cls', "^L"][os.name == 'nt'])

#"Wake up..." title/banner at the start
def ywu_banner(num_of_times):
    """
    Prints You wake up...(the game name) in ascii code big letters into a
    console and clears the screen using the clear function above and reprints
    the message to make the dots at the end appear to be moving.
    """
    print("__     __                         _")
    time.sleep(0.25)
    print("\ \   / /                        | |")
    time.sleep(0.25)
    print(" \ \_/ /__  _   _  __      ____ _| | _____   _   _ _ __")
    time.sleep(0.25)
    print("  \   / _ \| | | | \ \ /\ / / _` | |/ / _ \ | | | | '_ \ ")
    time.sleep(0.25)
    print("   | | (_) | |_| |  \ V  V / (_| |   <  __/ | |_| | |_) | _ _ ")
    time.sleep(0.25)
    print("   |_|\___/ \__,_|   \_/\_/ \__,_|_|\_\___|  \__,_| .__(_|_|_)")
    time.sleep(0.25)
    print("                                                  | |    ")
    time.sleep(0.25)
    print("                                                  |_| ")
    time.sleep(0.25)
    clear()

    for foo in range(num_of_times):
        print("__     __                         _")
        print("\ \   / /                        | |")
        print(" \ \_/ /__  _   _  __      ____ _| | _____   _   _ _ __")
        print("  \   / _ \| | | | \ \ /\ / / _` | |/ / _ \ | | | | '_ \ ")
        print("   | | (_) | |_| |  \ V  V / (_| |   <  __/ | |_| | |_) | _  ")
        print("   |_|\___/ \__,_|   \_/\_/ \__,_|_|\_\___|  \__,_| .__(_|_)")
        print("                                                  | |    ")
        print("                                                  |_| ")
        time.sleep(0.7)
        clear()

        print("__     __                         _")
        print("\ \   / /                        | |")
        print(" \ \_/ /__  _   _  __      ____ _| | _____   _   _ _ __")
        print("  \   / _ \| | | | \ \ /\ / / _` | |/ / _ \ | | | | '_ \ ")
        print("   | | (_) | |_| |  \ V  V / (_| |   <  __/ | |_| | |_) |  ")
        print("   |_|\___/ \__,_|   \_/\_/ \__,_|_|\_\___|  \__,_| .__(_)")
        print("                                                  | |    ")
        print("                                                  |_| ")
        time.sleep(0.7)
        clear()

        print("__     __                         _")
        print("\ \   / /                        | |")
        print(" \ \_/ /__  _   _  __      ____ _| | _____   _   _ _ __")
        print("  \   / _ \| | | | \ \ /\ / / _` | |/ / _ \ | | | | '_ \ ")
        print("   | | (_) | |_| |  \ V  V / (_| |   <  __/ | |_| | |_) |  ")
        print("   |_|\___/ \__,_|   \_/\_/ \__,_|_|\_\___|  \__,_| .___/")
        print("                                                  | |    ")
        print("                                                  |_| ")
        time.sleep(0.7)
        clear()

        print("__     __                         _")
        print("\ \   / /                        | |")
        print(" \ \_/ /__  _   _  __      ____ _| | _____   _   _ _ __")
        print("  \   / _ \| | | | \ \ /\ / / _` | |/ / _ \ | | | | '_ \ ")
        print("   | | (_) | |_| |  \ V  V / (_| |   <  __/ | |_| | |_) | _ _ ")
        print("   |_|\___/ \__,_|   \_/\_/ \__,_|_|\_\___|  \__,_| .__(_|_|_)")
        print("                                                  | |    ")
        print("                                                  |_| ")
        time.sleep(0.7)
        clear()

    START = input("Press ENTER/RETURN on your keyboard to start the game")
    time.sleep(0.7)
    clear()

Tags: andthetoinpygametimemain
1条回答
网友
1楼 · 发布于 2024-04-19 02:24:51

你的init()调用main.py需要在if __name__ == '__main__':块中,你应该而不是自己设置__name__Python将自动执行此操作。

否则,在调用init()之前,循环导入将导致helper无法正确导入。现在的顺序是这样的:

  1. 运行main.py,开始计算main.py。
  2. main.py中的import helper行开始计算helper.py。
  3. helper.py中的import main行启动main.py的另一个求值,因为main模块以前没有导入过。
  4. py的第二个求值中的import helper行将导入在#2开始导入helper时创建的现有helper对象,因此这不会创建无限循环-但现有helper对象尚未完全加载;它仍在等待main导入解决。
  5. main的第二次求值命中main.py底部的init()调用,该调用尝试运行init()-但helper仍未完成加载,因此helper.py中的way_out_quest函数尚未求值,因此未定义。

一般来说,最好一开始就避免循环进口,这样你就不必担心这样的问题。

相关问题 更多 >