0

In a text adventure game (python), I get user input for the next action and use regex to parse the input into two variables. The two variables are the verb and noun of the command, still in string form. For example, user input leads to...

final_noun = 'blow' final_verb = 'bubbles'

The regex is working correctly.

In another file, called actions, I have a list of functions for verbs/actions, including the blow function. I want to call this function up by purely using the final_noun and final_verb from regex. Also, I have the actual final verb object bubbles in the file GameFiles.

i tried: actions.final_verb(Gamefiles.final_noun)

which doesnt work because the inputs are still a string. i then tried using eval in many different ways, such as: actions.eval(final_verb(GameFiles.eval(remaining_noun)))

(This also didn't work)

Thank you for your time

2
  • 1
    final_noun = 'blow' final_verb = 'bubbles' Aren't those reversed? Commented Oct 31, 2020 at 2:49
  • @johnGordon yes you are correct, good catch, i mistakenly reversed those, but only when i posted this question, not in my original code. Commented Oct 31, 2020 at 3:21

1 Answer 1

1

I think you can do it by writing:

eval('actions.' + final_verb)(eval('GameFiles.' + remaining_noun))

given actions and Gamefiles are imported, which means you are able to run:

actions.blow(Gamefiles.bubbles)

However you should reconsider keeping your functions and data stored that way. A simple and a little bit better way to do this would be creating a dictionary with all your functions and objects and then accessing them through get_item:

verbs = {'blow' : actions.blow}
nouns = {'bubbles' : Gamefiles.bubbles}

verbs[final_verb](nouns[final_noun])
Sign up to request clarification or add additional context in comments.

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.