That's what execfile() is for.
http://docs.python.org/library/functions.html#execfile
Create a temporary file.
Write the content of the textbox into the file.
Close.
Execfile.
Delete when done.
That's what execfile() is for.
http://docs.python.org/library/functions.html#execfile
Create a temporary file.
Write the content of the textbox into the file.
Close.
Execfile.
Delete when done.
Python provides number of ways to do this using function calls: - eval() - exec()
For your needs you should read about exec.
Visual Studio Code - input function in Python - Stack Overflow
Python code to take user input and execute functions from that input - Stack Overflow
Pause a running python script and resume when necessary.
Python launcher runs code and immediately closes
you can add 'raw_input()' in the end - it will wait for a user input and will not close the window, untill you press enter
also have a look on Sublime Text 2 - it is a great text editor itself and have some IDE features
open you python code in it, press ctrl+b - it wil run the script and show you a console. you do not need raw_input() in this case
More on reddit.comHey, I would be curious if it is possible to read from an user any line of code and then run it from the Compiler?
The idea would be like: code = input() run(code)
and the user would write something like: Terminal: Enter a line of code: print(1+1)
the result would be 2
Thank you
Tasks are intended for building your application. Since Python is interpreted, you don't need to use tasks.json at all to run/debug your Python code. Use the launch.json instead. I am using Don Jayamanne's Python extension for debugging and have configured the launch.json as follows:
Open the Command Palette (Ctrl + Shift + P) and write the command:
start without debugging
Then select your Environment -> Click Python. This should create a launch.json file within a .vscode directory in the current directory.
Paste the following configuration json
{ "version": "0.2.0", "configurations": [ { "name": "Python", "type": "python", "request": "launch", "stopOnEntry": true, "pythonPath": "${config.python.pythonPath}", "program": "${file}", "debugOptions": [ "WaitOnAbnormalExit", "WaitOnNormalExit", "RedirectOutput" ], "console": "integratedTerminal" } ]}Save the file, open your python script in the editor and 'start without debugging' again. This should launch an integrated terminal where you can give input as well as see the output.
Ctrl+Shift+d, then choose integrated terminal/console.

def t1():
print(1)
def t2():
print(2)
def t3():
print(3)
def t4():
print(4)
def t5():
print(5)
def t6():
print(6)
# put the functions in a list
functions = [t1, t2, t3, t4, t5, t6]
def go(functions):
# .format(variable) replaces {} with the value of variable (string formatting)
number_of_funcs = len(functions)
greeting = "Type a number from 1 to {}: ".format(number_of_funcs)
selected = input(greeting)
try:
# index to start from
index = int(selected) - 1
except ValueError:
# check if the user wrote a number (exception handling)
print('Invalid input. Not a number')
return
if index > number_of_funcs - 1 or index < 0:
msg = 'Invalid input. Consider a number from 1 to {}'.format(number_of_funcs)
print(msg)
return
# iterate through the functions of the list
# starting from the index specified (list slicing)
for f in functions[index:]:
f()
while(True):
# infinite loop. press Ctrl+C to abort
go(functions)
See also
exception handling
string formatting
list slicing
You can put all your functions in a list and call sequentially all the functions that are contained in a slice of the list, as in this toy example
In [11]: l_of_functions = [ lambda x=x: print(x) for x in range(10)]
In [12]: for fun in l_of_functions[5:]: fun()
5
6
7
8
9
In [13]: for fun in l_of_functions[0:]: fun()
0
1
2
3
4
5
6
7
8
9
In [14]:
Addendum
In case the OP needs a function to get a number from a closed interval, here it is my attempt
In [28]: def ask_inside(minimum, maximum, max_tries=10, this_try=0):
...: answer = input('Give me a number comprised between %d and %d: '
...: %(minimum, maximum))
...: try:
...: number = int(answer)
...: except ValueError:
...: number = minimum-1
...: if minimum <= number <= maximum: return number
...: if this_try+1<max_tries:
...: return ask(minimum, maximum,
...: max_tries=max_tries, this_try=this_try+1)
...: else: print('You are boring')
...:
In [29]: ask_inside(1, 6, max_tries=3)
Give me a number comprised between 1 and 6: 2
Out[29]: 2
In [30]: ask_inside(1, 6, max_tries=3)
Give me a number comprised between 1 and 6: ojig
Give me a number comprised between 1 and 6: 0
Give me a number comprised between 1 and 6: 7
You are boring
In [31]:
Of course if you are on Python 2 print is a statement and input() โ raw_input().