By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,856 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,856 IT Pros & Developers. It's quick & easy.

question about function not executing

P: n/a
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()
--
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.
Nov 26 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hint:

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

Ara Kooser wrote:
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():
<clip>
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.
>>raw_input
<built-in function raw_input>
>>raw_input()
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()

--
Juho Schultz

Nov 26 '06 #2

P: n/a
Ara Kooser wrote:
<snip>
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
Nov 26 '06 #3

P: n/a
Ara Kooser wrote:
<snip>
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

Nov 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.