Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Thanked 0 Times in 0 Posts

    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:

    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('> '))

  2. AdSlot6
  3. #2
    Regular Coder Samhain13's Avatar
    Join Date
    Aug 2008
    Thanked 18 Times in 18 Posts
    Instead of using all "ifs", use "if" then "elif", and an "else". Like:

    # ...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.
            # 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 **


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts