[Tutor] global question
Ara Kooser
ghashsnaga at gmail.com
Sat Aug 11 23:40:18 CEST 2007
Hello,
I am back working on a text adventure game using functions. I broke
down the game into areas (modules) but I am having trouble getting the
other modules to recognize the globals I have set in the main program.
My main program looks like this:
####################################################################################
#A sample text adventure game using functions
#Version 1.0
#By Ara Kooser
#
####################################################################################
import random
import sys
import string
import outside1
import town1
import items
################################################################################
#
#Contains the character's information
#
################################################################################
stre = 9
con = 8
inte = 11
agl = 14
app = 10
mag = 6
sz = 9
hp = 17
reputation = 0
stealth = False
# Grass quest
quest1 = False
# Mushroom quest
quest2 = False
# Orc quest
quest3 = False
cursed = False
poison = False
diseased = False
equipment = {'Sword': 1, 'Dagger': 1, 'Walking staff': 1, 'Leather Armor':1}
backpack = {'Flint and steel': 1, 'Rations': 7, 'dressing kit': 6,
'waterskin': 2}
belt_pouch = {}
money = {'ducats': 50}
Forage = 45
Haggle = 33
Stealth = 28
Fishing = 58
Herbalism = 48
Climb = 52
Sword = 34
Staff = 47
Dagger = 42
Field_Dressing = 62
#####################################################################################
# help function that will give you a list of commands
def help():
print "look, examine (object), n, w, e, s, take (item)"
print "climb, stealth, fishing, herbalism, forage, haggle"
print "aid (applies first aid)"
print "wield (object), attack, flee, close, withdraw, maintain"
print "inv (returns inventory list), cs"
print "Example: examine book, take ducats, attack orc"
def character_sheet():
print """\
============================================================================
Name: Profession: Apprentice
Social Class: Low Race: Human
============================================================================
Strength 9
Constitution 8
Intelligence 11
Agility 14
Appearance 10
Magic 6
Size 9
============================================================================
Skills: Forage, Haggle, Stealth, Fishing, Herbalism, Climb, Sword, Staff,
Dagger, Field Dressing
============================================================================
"""
The other modules are outside1, etc..
outside1 looks like this
def outside1():
print " Current Hit Points = ",hp
print " Current Reputation = ",reputation
print equipment
print backpack
......
When I run this I get:
Traceback (most recent call last):
File "/Users/ara/Desktop/text_advent/outside1.py", line 452, in <module>
outside1()
File "/Users/ara/Desktop/text_advent/outside1.py", line 26, in outside1
print " Current Hit Points = ",hp
NameError: global name 'hp' is not defined
I kind of suspect the global name I set on main wouldn't work for the
outside1 module. Is there a way to do this. I don't want to copy the
globals into each area file because they need to be updated
during this course of the game. Thanks for any help or suggestions.
Ara
--
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.
More information about the Tutor
mailing list