0

i'm running into "out of range" while hitting enter and imputing single words. I understand why they are out of range, i just can't find how to fix the issue. this is my code:

commands = {
'help': help,
'exit': exit,
'look': look,
'stats': thePlayer.printStats,
's':thePlayer.move_South}

def runCmd(cmd, args, player):
    commands[cmd](args, player)

def help(args):
    print(commands)

def play():
    main1()
    World.loadTiles()
    #These lines load the starting room and display the text
    room = World.tileExists(player.locationX, player.locationY)
    print(room.introText())
    while player.isAlive():
        room = World.tileExists(player.locationX, player.locationY)
        room.modifyPlayer(player)
        # Check again since the room could have changed the player's state
        if player.isAlive():
            print("\nHp:%d\%d  Mp:%d\%d\n"%(player.hp,player.maxHp,player.mp,player.maxMp))
            print(room.printEnemy())
            availableActions = room.availableActions()            
            for action in availableActions:
                print(action)
            actionInput = input('Action: ')
            action = actionInput.lower()
            action = action.split()            
            print(action)
        if action[0] in commands:
            runCmd(action[0],action[1], player)

i realize i'm not passing an "action[1]" and that just hitting enter isn't in "commands" causing my error. i'm new to coding so creating a game is how i'm teaching myself.

i'm trying to get to where i can type: enter, single word (help, stats), double words (look rat), other things with more words ( buy big sword) and so on. any help on how i can code this correctly?

1 Answers1

0

How about testing

len(action)

before calling runCmd? Thus, you can know if there are one word, two words, or more than two words. If there is only one word, you can call:

runCmd(action[0], None, player)

If there are two words, you can call your current function. If there are more than two words, it's a bit tricky. You can pass the whole action variable. Then, the called function has to know how many parameters it needs.

Abrikot
  • 965
  • 1
  • 9
  • 21
  • that would be a very brute force method to make it work. but, that would be adding 20 some times of extra code that i'm sure can be simplified. i know there is a *args i could pass, instead which (to my knowledge) will pass as my arguments as i want, however i don't know how exactly to use it yet. – Reederboard May 24 '17 at 07:27
  • I have never used the splat operator, so I can't help you. Perhaps you can find some clues [here](https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/). It's Python 2, but I think it works in Python 3, too. – Abrikot May 24 '17 at 07:36