Title: Execute function when certain word is found in string in python?
Tags: python;pseudocode
Question: I'm working on a program for my school's programming outreach club, What I'm trying to do is have the user type in a string and the program searches the string. For example, they would type something like
moveInstant(4, 8)
and it would execute function
def moveInstant(targetX, targetY)
pyautogui.moveTo(targetX, targetY)
Make sense? basically taking a long string and converting each instance of pseudo code into a predefined function with arguments. The idea is that a user could input a full txt file and it could be executed by the program.
Comment: https://docs.python.org/3.5/library/functions.html#eval

Here is the accepted answer: I am curious to know what the pseudo code will look like or how you are going to implement the 'string matching'. But:
Once you got the right function names and parameters, you can just use the exec statement to execute it. If not only the side effect but the return value of a function is what you want, use eval (Differences). Or,
Do some clean-up job for the text file and execute it using the execfile function.
Noted that statements like eval or exec are potentially harmful, and that's why your 'string matching' for untrusted inputs is important.

Here is another answer: What you're looking for is the eval() method.
Python Docs
Example:
&gt;&gt;&gt; def test(x):
...   print(x)
&gt;&gt;&gt; test(3)
&gt;&gt;&gt; 3
&gt;&gt;&gt; y = "test(124)"
&gt;&gt;&gt; eval(y)
&gt;&gt;&gt; 124

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:07/10/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><pseudocode>', 'Title': 'Execute function when certain word is found in string in python?', 'ViewCount': 226, 'id': 45019310, 'user_AboutMe': None, 'user_AccountId': 10948624.0, 'user_DisplayName': 'Max Fieschko', 'user_Reputation': 37.0, 'user_Views': 13.0, 'user_id': 8045709.0}}