我如何 运行 由 python 中的用户编码 given/taken
How do i run code given/taken by the user in python
我是 Kotlin 的新手,我正在尝试在 Kotlin 中创建一个项目来执行用户获取的代码。
我在 python 中使用 exec()
来执行代码,但我也不知道如何使用它,如果你知道 python 和 Kotlin 请帮助我,请帮助
任何帮助将不胜感激!
谢谢
编辑:这里有更多信息..我是 Kotlin 的新手
我需要一些命令的帮助,这些命令类似于 python 中的 exec
我正在为 Kotlin 程序中的 运行 python 命令编写程序
这是实验性的..
请原谅我,因为我是 Whosebug 的新手。
谢谢..
我有一个想法
您可以获得用户输入并将它们保存在文件中。
然后通过导入'os'库
在shell上执行保存的文件
import os
print("Enter any python commands!")
print("Enter 'run' to execute.")
user_inputs = ''
# Get user input until user enter 'run' phrase
while True:
user_input = input('> ')
if user_input == 'run': break
user_inputs = user_inputs + "\r\n" + user_input
# save user input on a file
file = open("run.py", 'w')
file.writelines(user_inputs)
file.close()
# execute the code on terminal and get output, then display to the user
stream = os.popen('python run.py')
output = stream.read()
print("---------------------------------")
print(output)
编码愉快
我是 Kotlin 的新手,我正在尝试在 Kotlin 中创建一个项目来执行用户获取的代码。
我在 python 中使用 exec()
来执行代码,但我也不知道如何使用它,如果你知道 python 和 Kotlin 请帮助我,请帮助
任何帮助将不胜感激!
谢谢
编辑:这里有更多信息..我是 Kotlin 的新手 我需要一些命令的帮助,这些命令类似于 python 中的 exec 我正在为 Kotlin 程序中的 运行 python 命令编写程序 这是实验性的.. 请原谅我,因为我是 Whosebug 的新手。 谢谢..
我有一个想法 您可以获得用户输入并将它们保存在文件中。 然后通过导入'os'库
在shell上执行保存的文件import os
print("Enter any python commands!")
print("Enter 'run' to execute.")
user_inputs = ''
# Get user input until user enter 'run' phrase
while True:
user_input = input('> ')
if user_input == 'run': break
user_inputs = user_inputs + "\r\n" + user_input
# save user input on a file
file = open("run.py", 'w')
file.writelines(user_inputs)
file.close()
# execute the code on terminal and get output, then display to the user
stream = os.popen('python run.py')
output = stream.read()
print("---------------------------------")
print(output)
编码愉快