1

main.py

import engine
engine.objects['key'] = "It's a key"
engine.main()

engine.py

# inventory = []

objects = {

}

def main():
    while True:
        choice = raw_input(">>: ")
        command, obj = choice.split()
        if command == 'examine':
            if obj in objects:
                print objects[obj]
            else:
                print 'joking right?'
        else:
            print 'joking right?'

When I type "examine" with no second word, the parameter, it gives me an error.

>>: asdf
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    engine.main()
  File "C:\Users\Patrick\Documents\Programming\Game engine test\engine.py", line
 11, in main
    command, obj = choice.split()
ValueError: need more than 1 value to unpack
Press any key to continue . . .

I understand why, but how do I fix it?

2 Answers 2

3

So we can eliminate the raw_input() from the problem, and boil this down to the following lines:

choice = 'examine'
result = choice.split()      # result == ['examine']
command, obj = result        # Boom.

The return value from split() is a list. When you only have one word (separated by spaces), like "examine", the list contains only one element.

Then, when you try to to unpack that list into command and obj, python says, "I can't do that. You're expecting two entries, but I only have one."

What you need to do is perform some intermediate checking:

args = choice.split()
if len(args) < 2:
    print 'Invalid command.'
    continue
command, obj = args
# ...
Sign up to request clarification or add additional context in comments.

4 Comments

You'll need a little bit more - as it is, it will still boom - just after printing "Invalid command" :-)
@SeanVieira: Added a continue, thanks. I wanted to add more logic, but I decided to stick with fixing the OP's particular issue, as opposed to designing a text-based menu interface for the him.
Thank you! This really helped :D I can now finish my game with a new game engine :D
@user1564265, you're welcome. Remember, if an answer actually answers your question, you should consider accepting it. (This and your other questions as well).
0
args = choice.split()
command = args[0]
#Branch according to command here
#Most likely you will want to separate them into functions
if command == 'examine':
    if len(args) < 2:
       print 'Missing argument'
    else:
       obj = args[1]
       if obj in objects:
           print objects[obj]
       else:
           print 'joking right?'
else:
    print 'joking right?'

Also you might look at the cmd module in the python standard library. You can basically create an object with do_<MYCOMMANDNAME> functions and it will issue a command prompt and looks at the first word as a command and look for a corresponding do_command function to execute with the rest of the arguments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.