gog*_*be2 5 python module file attributeerror traceback
我一直在学习 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'
Run Code Online (Sandbox Code Playgroud)
这是主文件(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()
Run Code Online (Sandbox Code Playgroud)
助手.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)
Run Code Online (Sandbox Code Playgroud)
这是包含所有 art/clear 和 text 功能和内容的 assci 文件(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()
Run Code Online (Sandbox Code Playgroud)
您init()在通话main.py的需求是在一个if __name__ == '__main__':块,你应该不设置__name__自己- Python会自动做到这一点。
否则您的循环导入将导致helper在init()调用之前无法正确导入。目前的顺序是这样的:
import helpermain.py 中的行开始正在评估 helper.py。import mainhelper.py 中的行开始对 main.py 进行另一次评估,因为该main模块之前尚未导入。import helper在main.py进口时被导入#2开始帮手被创造了,所以这不会产生无限循环现有的辅助对象的第二次评估行-但现有的辅助对象还没有完全加载尚未; 它仍在等待main导入解决。main命中init()main.py 底部的调用,该调用尝试运行init()- 但helper仍未完成加载,因此way_out_questhelper.py 中的函数尚未评估,因此未定义。一般来说,最好首先避免循环导入——这样你就不必担心这样的问题。