Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-16-2011, 11:00 AM   PM User | #1
Mark2
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Mark2 is an unknown quantity at this point
Question Problem w/ text adventure

I am making a simple text adventure and my main problem is that whenever I type in a response it either responds correctly or with location[0] (which is the kitchen in this case). I was wondering how to correctly set out the code so that it functions correctly by stating the location[0] and then responding.

Here is the games source:


Code:
item = ('...', 'sword','emerald')
kitchen = ('Kitchen','A small room with a stove and cupboard. There is a single door to the north.', item[2], item[0], item[0])
bathroom = ('Bathroom','An old smelly room with with a sink, bath and a variety of rats and spiders. To the south there is a single door.', item[0], item[0], item[0])

room_options = {
    kitchen: (bathroom,),
    bathroom: (kitchen,),
    }

command = ('help', 'items', 'take', 'drop', 'inventory', 'look', 'go')
directions = ('north', 'south', 'east', 'west')

location = kitchen

while True:
     print location[0]
     commandinput = raw_input('> ')

     if commandinput == command[0]:
         print 'Commands'
         print 'Help, items, take, drop, inventory, look, go'
         commandinput = (raw_input('> '))

     if commandinput == command[1]:
         print 'There is a:'
         print location[2]
         print location[3]
         print location[4]
         commandinput = (raw_input('> '))

     if commandinput == command[2] + ' ' + location[2]:
         print 'You take a ' + location[2]
         commandinput = (raw_input('> '))
         if location[2] == item[0]:
             print 'You cant take nothing.'
             commandinput = (raw_input('> '))

     if commandinput == command[2] + ' ' + location[3]:
         if location[3] == item[0]:
             'You cant take nothing.'
             commandinput = (raw_input('> '))
Mark2 is offline   Reply With Quote
Old 06-18-2011, 02:54 PM   PM User | #2
Samhain13
Regular Coder

 
Samhain13's Avatar
 
Join Date: Aug 2008
Location: Pilipinas
Posts: 165
Thanks: 4
Thanked 18 Times in 18 Posts
Samhain13 is on a distinguished road
Instead of using all "ifs", use "if" then "elif", and an "else". Like:

Code:
# ...all the code before the while
while True:
    if commandinput == command[0]:
        # whatever's supposed to be here.

    elif commandinput == command[1]:
        # keep repeating this for all other recognised commands
        # and combinations.

    else:
        # This fires off if the command isn't something that we accept.
        print location[0]
        commandinput = raw_input("> ")
__________________
I am a Man of Truth. I am a Free Human Person. I am a Peacemaker.
** Independent Multimedia Artist in Pasig **
Samhain13 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:41 AM.


Advertisement
Log in to turn off these ads.