编程语言
首页 > 编程语言> > 为什么我不断在python中收到这个大错误.追溯(最近一次呼叫过去)…和AttributeError

为什么我不断在python中收到这个大错误.追溯(最近一次呼叫过去)…和AttributeError

作者:互联网

我一直在学习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()

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

这是具有所有art / clear和text函数以及内容(assci.py)的assci文件:

#-------------------------------------------------------------------------------
# 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()

解决方法:

您在main.py中的init()调用必须位于if __name__ ==’__main__’:块中,并且您不应该自己设置__name__-Python将自动执行此操作.

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

>您运行main.py,这将开始评估main.py.
> main.py中的导入帮助器行将开始评估helper.py.
> helper.py中的import main行开始了对main.py的另一次评估,因为先前尚未导入main模块.
> main.py的第二次评估中的import helper行将导入在#2开始导入helper时创建的现有helper对象,因此这不会创建无限循环-但是现有的helper对象尚未完全加载;它仍在等待主要进口商品解决.
> main的第二次评估命中了main.py底部的init()调用,该调用尝试运行init()-但是helper仍未完成加载,因此helper.py中的way_out_quest函数尚未完成尚未评估,因此未定义.

通常,最好首先避免循环导入-这样就不必担心此类问题.

标签:attributeerror,module,file,traceback,python
来源: https://codeday.me/bug/20191122/2059105.html