鱼C论坛

 找回密码
 立即注册
查看: 1390|回复: 5

[技术交流] Python 代码执行器改进版

[复制链接]
发表于 2020-2-22 18:19:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 一个账号 于 2020-3-23 16:18 编辑

原版来源于 "zltzlt" 发表的帖子:Python 代码执行器【模拟 Python Shell】

运行效果:

  1. Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
  2. Type "help", "copyright", "credits" or "license()" for more information.
  3. >>>
  4. ===================== RESTART: E:\Python\程序\Python 代码执行器.py ====================
  5. ------------------------------ Python 3.8.1 ------------------------------
  6. >>> print("Hello")
  7. Hello
  8. >>> 123
  9. 123
  10. >>> 1+1
  11. 2
  12. >>> _
  13. 2
  14. >>> name = input(); print(name)
  15. whxwxh
  16. whxwxh
  17. >>> def f():
  18. ...         print("f()")
  19. ...
  20. >>> f()
  21. f()
  22. >>> import sys
  23. >>> sys.ps1 = "--> "
  24. --> sys.ps2 = "=== "
  25. --> def f():
  26. ===         print("hello world")
  27. ===
  28. --> f()
  29. hello world
  30. -->
复制代码


源代码:

游客,如果您要查看本帖隐藏内容请回复


本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-22 19:10:19 From FishC Mobile | 显示全部楼层
和自带shell一样的吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-22 19:18:40 | 显示全部楼层
  1. >>> if 1:
  2. >>> Traceback (most recent call last):
  3.   File "E:/Programming/Python/项目/demo3.py", line 31, in <module>
  4.     exec("\n".join(lines))
  5.   File "<string>", line 1
  6.     if 1:
  7.         ^
  8. SyntaxError: unexpected EOF while parsing
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-22 20:45:27 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-22 20:47:26 | 显示全部楼层


>>> 跑到前面去了

  1. ------------------------------ Python 3.8.1 ------------------------------
  2. >>> if 1:
  3. ... 123123
  4. ...
  5. >>> Traceback (most recent call last):
  6.   File "E:/Programming/Python/项目/demo3.py", line 26, in <module>
  7.     exec("\n".join(lines))
  8.   File "<string>", line 2
  9.     123123
  10.     ^
  11. IndentationError: expected an indented block
复制代码


代码为什么要刻意加这么多空行?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-22 21:31:21 | 显示全部楼层
zltzlt 发表于 2020-2-22 20:47
>>> 跑到前面去了

在我这里不是这样的:

  1. Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
  2. Type "help", "copyright", "credits" or "license()" for more information.
  3. >>>
  4. ===================== RESTART: E:\Python\程序\Python 代码执行器.py ====================
  5. ------------------------------ Python 3.8.1 ------------------------------
  6. >>> if 1:
  7. ... 123123
  8. ...
  9. Traceback (most recent call last):
  10.   File "E:\Python\程序\Python 代码执行器.py", line 25, in <module>
  11.     exec("\n".join(lines))
  12.   File "<string>", line 2
  13.     123123
  14.     ^
  15. IndentationError: expected an indented block
  16. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 00:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表