|通过固定的英语句式,让其在学习英语的同时培养编程思维的可行性讨论。

初学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)   # 仅原型演示

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐