question about function not executing

A

Ara Kooser

Hello all,

I hope I am posting this correctly. I am running Python 2.4.2 on
Slackware 11.0 using IDLE. I am in the process of learning python so I
am writing a text adventure game using functions and global variables
only. I know there is a better way to do this but that is for later.

When I run the python program it works fine until I try to go west
from my inital start room. I get the room description but no raw_input
prompt. I just get dumped back to >>> in the python shell. I think I
am missing something simple. I pasted in the code below. I am using
gmail so I hope the formatting stays. If not I can send the .py as an
attachment if needed. Thanks.

Ara

Code:


#A sample text adventure game
#Version 0.1
#By Ara Kooser


#Using global variables for the character
#Want to use a function instead

stre = 9
con = 8
inte = 11
agl = 14
app = 10
mag = 6
sz = 9

hp = 17

reputation = 0

stealth = False

quest1 = False
quest2 = False

curse = False
poison = False
diseased = False

ducats = 50
lira = 25
florin = 80


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 = {}

#####################################################################################

day = False


# 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 "field dressing"
print "wield (object), attack, flee, close, withdraw"
print "backpack, belt pouch, cs"
print "Example: examine book, take ducats, attack orc"

# I want to setup the character has a function but I am not sure how
# to call and modify, the stats, counters, and flags. Currently I am
# setting these as global variables

#def character(self):
# #Initalize stats
# self.str = 9
# self.con = 8
# self.int = 11
# self.agl = 14
# self.app = 10
# self.mag = 6
# self.sz = 9

# self.hp = 17

# #Initalize flags
# self.quest1 = False
# self.quest2 = False

# self.curse = False
# self.poison = False
# self.diseased = False

#Initalize counters
# self.ducats = 50
# self.lira = 25
# self.florin = 80
# self.reputation = 0


# Sets up a character sheet to print out to screen
#To do list - make the sheet update as the game progresses
def character_sheet():
print """\
============================================================================
Name: Profession:
Social Class: Race:

============================================================================
Strength
Constitution
Intelligence
Agility
Appearance
Magic
Size
============================================================================
Ducats: Lira: Florin:

Skills: Forage, Haggle, Stealth, Fishing, Herbalism, Climb, Sword, Staff,
Dagger, Field Dressing

Equipment: Backpack, Belt Pouch, Run of the Mill Sword, Dagger, Flint&Steel
1 week food, 2 waterskins, walking stick, dressing kit
============================================================================
"""



def start():
print '''
SAMPLE TEXT ADVENTURE V0.1
You are the last person to leave the small village of Hommlet. The wooden
gate closes behind you and twilight reaches across the land. A dense mist
creeps up out of the ground, only to be kept at bay by the watchmens torches.
Somewhere deep in the woods lies the foul orcs you have tracked for several
days.

'''

print

def outside1():
global hp
global reputation
print " Current Hit Points = ",hp
print " Current Reputation = ",reputation
print ''' You are outside the town gates. The dirt road heads
(N)orth to another
town several days away. The forest lies (E)ast and (W)est through the
meadows. The
rumors you heard in town describe the orcs as being to the west. The town's gate
is to the (S)outh but it is locked for the night.
Type 'help' for a full list of commands.'''
print
prompt_out1()

def out1_desc():
print ''' The fog is growing denser as the sun sets on the meadows.
The exits are (N)orth, (E)ast and (W)est.'''
print
prompt_out1()

def prompt_out1():
global day
prompt_o1 = raw_input("Type a command: ").lower()
try:

if prompt_o1 == "help":
help()
print
prompt_out1()

elif prompt_o1 == "cs":
character_sheet()
print
prompt_out1()

elif prompt_o1 == "status":
print " Current Hit Points = ",hp
print " Current Reputation = ",reputation
prompt_out1()

elif prompt_o1 == "backpack":
print backpack
prompt_out1()

elif prompt_o1 == "belt pouch":
print belt_pouch
prompt_out1()

elif prompt_o1 == "equipment":
print equipment
prompt_out1()

########################################################################################
elif prompt_o1 == "examine fog":
print "The fog seems natural enough for this time of year."
print
prompt_out1()

elif prompt_o1 == "examine road":
print ''' The road is a well travelled dirt road
winding many leagues'''
print
prompt_out1()

elif prompt_o1 == "look":
out1_desc()

#######################################################################################
elif prompt_o1 == "w":
meadow1()

elif prompt_o1 == "e":
meadow2()


elif prompt_o1 == "s":
#if day = False
print "The town's gate is closed for the night"
print
prompt_out1()
#elif
# town_1

elif prompt_o1 == "n":
n_road()

######################################################################################
elif prompt_o1 == "haggle":
print "There is no one to haggle with here."
promt_out1()

elif prompt_o1 == "stealth":
print "You try and be stealthy"
prompt_out1()

else:
print "Please choose another command. That command is invalid"
print
prompt_out1()

except ValueError:
print "Please choose another command. That command is invalid"
print
prompt_out1()


def meadow1():
global hp
global reputation
print " Current Hit Points = ",hp
print " Current Reputation = ",reputation
print ''' You are in the meadows that surrond the northwest of
the town. In the day the
field is a golden brown. Right now the grass is waist high and thickly
covered with dew. Your
footsteps and the sounds of the night are dampened here.
A broken trail leads into the forest (W)est. The meadow continues
(N)orth along the road.
(E)ast takes you back to the road and the entrance to Hommlet. The
wooden wall of the town is
just south of here. Type 'help' for a full list of commands.'''
print
prompt_meadow1

def meadow1_desc():
print ''' The fog bends the grass down and drops of water roll
off the blades. You can see
about 5 paces in front of you. Your clothes are starting to get damp.
The broken trail heads (W)est.
The road is (E)ast. The meadow continues (N)orth and the town's wall
is to the south.'''
prompt_meadow1

def prompt_meadow1():

prompt_m1 = raw_input("Type a command: ").lower()
try:

if prompt_m1 == "help":
help()
print
prompt_meadow1()

elif prompt_m1 == "cs":
character_sheet()
print
prompt_meadow1()

elif prompt_m1 == "status":
print " Current Hit Points = ",hp
print " Current Reputation = ",reputation

elif prompt_m1 == "backpack":
print backpack
prompt_meadow1()

elif prompt_m1 == "belt pouch":
print belt_pouch
prompt_meadow1()

elif prompt_m1 == "equipment":
print equipment
prompt_meadow1()

###############################################################################################

elif prompt_m1 == "examine fog":
print "The fog seems natural enough for this time of year."
print
prompt_meadow1()

elif prompt_m1 == "examine grass":
print '''The grass would be a golden brown in the light
however now it's just
gray and damp.'''
print
prompt_meadow1()

elif prompt_m1 == "look":
meadow1_desc()

###############################################################################################

elif prompt_m1 == "s":
#if day = False
print "The town's wooden wall is in your way"
print
prompt_meadow1()
#elif
# town_1

elif prompt_m1 == "w":
edge_forest1()

elif prompt_m1 == "e":
outside_1()

elif prompt_m1 == "n":
n_meadow_1()

################################################################################################

elif prompt_m1 == "haggle":
print "There is no one to haggle with here."
promt_meadow1()

elif prompt_m1 == "stealth":
print "You try and be stealthy"
prompt_meadow1()

elif prompt_m1 == "forage":
if getGrass_m1 == 0:
print "You find some blades of grass and gather them up."
backpack['mdw_grass']
backpack['mdw_grass'] = 1
print backpack
getGrass == 1
prompt_meadow1()
elif getGrass_m1 == 1:
print "Someone has gathered the grass already."
print
prompt_meadow1()


else:
print "Please choose another command. That command is invalid"
print
prompt_meadow1()

except ValueError:
print "Please choose another command. That command is invalid"
print
prompt_meadow1()


start()
outside1()
 
J

Juho Schultz

Hint:

Posting only the piece of code causing the problem will give you more
answers...

Ara said:
Hello all,

I hope I am posting this correctly. I am running Python 2.4.2 on
Slackware 11.0 using IDLE. I am in the process of learning python so I
am writing a text adventure game using functions and global variables
only. I know there is a better way to do this but that is for later.

When I run the python program it works fine until I try to go west
from my inital start room. I get the room description but no raw_input
prompt. I just get dumped back to >>> in the python shell. I think I
am missing something simple. I pasted in the code below. I am using
gmail so I hope the formatting stays. If not I can send the .py as an
attachment if needed. Thanks.

Ara

<clip>

Giving the command 'w', you call meadow1()
elif prompt_o1 == "w":
meadow1()
def meadow1():
print
prompt_meadow1

So you end up here, the meadow1() function returns to the
except ValueError:
and ends then as expected.
def meadow1_desc():
prompt_meadow1

Same problem would occur in here.

I guess you want to call this:
def prompt_meadow1():

prompt_m1 = raw_input("Type a command: ").lower()

So write
prompt_meadow1()
instead of
prompt_meadow1

(experiment in python shell and you see the difference.now python waits for your input

For the player, create a class.

class player(object):

def __init__(self):
self.poisoned = False

def take_poison(self):
print 'You are poisoned'
self.poisoned = True
# effects of poison in here:
# take some hitpoints
# maybe reduce some stats
# and so on...

# now, generate a player instance
p = player() # calls __init__
# poison the player
p.take_poison()
 
J

Jussi Salmela

Ara Kooser wrote:
When I run the python program it works fine until I try to go west
from my inital start room. I get the room description but no raw_input
prompt. I just get dumped back to >>> in the python shell. I think I
am missing something simple. I pasted in the code below. I am using

<snip>

You've got at least 2 places that read
prompt_meadow1
instead of
prompt_meadow1()
like they should.

Cheers,
Jussi
 
J

Jussi Salmela

Ara Kooser wrote:
When I run the python program it works fine until I try to go west
from my inital start room. I get the room description but no raw_input
prompt. I just get dumped back to >>> in the python shell. I think I
am missing something simple. I pasted in the code below. I am using

<snip>

You've got at least 2 places that read
prompt_meadow1
instead of
prompt_meadow1()
like they should.

Cheers,
Jussi
 
D

Dennis Lee Bieber

Hello all,

I hope I am posting this correctly. I am running Python 2.4.2 on
Slackware 11.0 using IDLE. I am in the process of learning python so I
am writing a text adventure game using functions and global variables
only. I know there is a better way to do this but that is for later.
I suspect you'd be better off to /start/ with one of those "better
way"s... Long lists of if/elif blocks is not the most maintainable, and
you have to maintain character state between "rooms"... I do hope you
are looking at just a single-player program... (If multi-player, I'd
have suggested looking at
http://www.strout.net/python/poo/
but that hasn't been really updated in decades). OTOH,
http://py-universe.sourceforge.net/
seems to still be active, and looking at any available source code might
be useful.
When I run the python program it works fine until I try to go west
from my inital start room. I get the room description but no raw_input
prompt. I just get dumped back to >>> in the python shell. I think I
am missing something simple. I pasted in the code below. I am using
gmail so I hope the formatting stays. If not I can send the .py as an
attachment if needed. Thanks.

def meadow1():
global hp
global reputation
print " Current Hit Points = ",hp
print " Current Reputation = ",reputation
print ''' You are in the meadows that surrond the northwest of
the town. In the day the
field is a golden brown. Right now the grass is waist high and thickly
covered with dew. Your
footsteps and the sounds of the night are dampened here.
A broken trail leads into the forest (W)est. The meadow continues
(N)orth along the road.
(E)ast takes you back to the road and the entrance to Hommlet. The
wooden wall of the town is
just south of here. Type 'help' for a full list of commands.'''
print
prompt_meadow1
prompt_meadow1()
def meadow1_desc():
print ''' The fog bends the grass down and drops of water roll
off the blades. You can see
about 5 paces in front of you. Your clothes are starting to get damp.
The broken trail heads (W)est.
The road is (E)ast. The meadow continues (N)orth and the town's wall
is to the south.'''
prompt_meadow1
same problem


I'd strongly suggest, at the least, devising a data structure for
locations... something like:

locations = { "meadow1" : {"description" : "main description",
"n" : {"look description" : "looking north",
"move action" : "north field"},
"e" : ... ,
"s" : ... ,
"objects" : {"axe" : {"description" : "it's
an axe",
"takeable" : True,
"breakable" : False },
"jade figure" : { ..., } }

"north field" : {"description" : "north field description",
.... }
}

You'd then have a "character" attribute of "current_location"... a
"look" with no arguments would do something like:

print locations[current_location]["description"]
print "You see:"
for itm in locations[current_location]["objects"].keys():
print locations[current_location]["objects"][itm]["description"]

For a "look xxx" (ie, with an argument) the simplest is to assume
direction (use "examine xxx" for objects in inventory, and assume you
have to pick up the object in the room, putting it in your inventory,
before you can examine it)

direction = "north"

print "Looking %s, you see %s" % (direction,
locations[current_location].get(direction[0], {"look
description" : "nothing"})["look description"])

To move rooms:

"go xxx" (or just "xxx")

direction = "north"
new_location = locations[current_location].get(direction[0], {"move
action" : None})["move_action"]
if new_location:
current_location = new_location
print locations[current_location]["description"]
print "You see:"
for itm in locations[current_location]["objects"].keys():
print
locations[current_location]["objects"][itm]["description"]
else:
print "You can not move in that direction"


With such a data structure, you main processing looks something
like:

NOISE_WORDS = ["at", "the", "on"]
COMMAND_VERBS = ["throw", "take", "drop", "go", "look" ]
while not Done:
command = raw_input("Enter your command: ").lower().split()
# say input was "throw the axe at the bear"
command = [wrd for wrd in command if wrd not in NOISE_WORDS]
# filtered down to ["throw", "axe", "bear"]
if command[0] not in COMMAND_VERBS or command[0] == "go":
#assume it is a raw direction to move
if command[0] == "go":
direction = command[1]
else:
direction = command[0]
new_location = locations[current_location].get(direction[0],
{"move action" : None})["move_action"]
if new_location:
current_location = new_location
print locations[current_location]["description"]
print "You see:"
for itm in locations[current_location]["objects"].keys():
print
locations[current_location]["objects"][itm]["description"]
else:
print "You can not move in that direction"

elif command[0] == "look":
....


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dennis Lee Bieber

I suspect you'd be better off to /start/ with one of those "better
way"s... Long lists of if/elif blocks is not the most maintainable, and
you have to maintain character state between "rooms"... I do hope you

I should also mention that, by calling a function for each move you
are nesting function calls on the stack, and will eventually run out of
stack space.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top