I have been learning python and getting different things off the internet and putting them all into this game I am making: "You wake up..." It's a text based assci RPG (Roll playing game). It worked until I got this error:
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'
This is the main file (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)
This is the assci file with all the art/clear and text functions and stuff (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()
Traceback upon exception The return value of the print() function is a Python None object. And adding something to None will trigger an exception. The lines starting with “Traceback (most recent call last):” are the traceback. It is the stack of your program at the time when your program encountered the exception.
The error message line of the NameError traceback gives you the name that is missing. In the example above, it's a misspelled variable or parameter to the function that was passed in.
You can set sys. tracebacklimit = 0 , or use sys. excepthook to ignore the traceback itself.
Some of the common traceback errors are:KeyError. TypeError. valueError. ImportError /ModuleNotFound.
Your init()
call in main.py
needs to be in an if __name__ == '__main__':
block, and you should not set __name__
yourself - Python will do that automatically.
Otherwise your circular imports will cause helper
to not be imported properly before init()
is called. The sequence as it stands is going like this:
import helper
line in main.py starts helper.py being evaluated.import main
line in helper.py starts another evaluation of main.py because the main
module has not been previously imported.import helper
line in the second evaluation of main.py imports the existing helper object that was created when #2 started helper being imported, so this doesn't create an infinite loop - but the existing helper object hasn't fully loaded yet; it's still waiting for the main
import to resolve.main
hits the init()
call at the bottom of main.py, which tries to run init()
- but helper
still isn't done loading, and thus the way_out_quest
function in helper.py hasn't been evaluated yet and thus is not defined.In general, it's best to avoid circular imports in the first place - then you don't have to worry about problems like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With