培养中小学生编程思维(指令——执行)的自然语言编程设想(用deepseek简单写的模型 附源码)
|通过固定的英语句式,让其在学习英语的同时培养编程思维的可行性讨论。
初学Python,对于自然语言模型的初步探索,期望各位大佬批评指正。
句式 对应的操作
Create a list called "fruits". 新建空列表
Add "apple" to fruits. 添加元素
Print all items in fruits. 打印全部
Check if "apple" is in fruits. 判断存在
Repeat 3 times: Print "hi". 循环执行
import re
lists = {}
cmd = input("> ")
if cmd.startswith("Create a list called"):
name = cmd.split("called")[-1].strip('" .')
lists[name] = []
elif cmd.startswith("Add") and "to" in cmd:
parts = cmd.split("to")
val = parts[0].replace("Add","").strip('" ')
name = parts[1].strip()
if name in lists: lists[name].append(val)
elif cmd.startswith("Print all items in"):
name = cmd.replace("Print all items in","").strip()
print(lists.get(name, []))
elif re.match(r"Repeat \d+ times:", cmd):
count = int(re.search(r"\d+", cmd).group())
sub = cmd.split(":",1)[1]
for _ in range(count):
exec(sub) # 仅原型演示
更多推荐



所有评论(0)