AI 编程提示词的实战写法
分享高效编程提示词的具体技巧和案例。帮助开发者更好地利用 AI 工具,包含可直接应用的方法。
分享高效编程提示词的具体技巧和案例。帮助开发者更好地利用 AI 工具,包含可直接应用的方法。
如果你刚开始学习编程,可能听说过 ChatGPT、GitHub Copilot 或 Cursor 这类可以帮助编写代码的 AI 工具。也许你已经尝试过,却因为它们给出的代码令人费解或者根本无法运行而感到沮丧。
事实是:AI 的能力非常强大,但它不会读心。秘诀不只是让 AI 编写代码,更在于学会如何正确地提问。
可以这样理解:如果你走到一位资深程序员面前,对他说“帮我做一个网站”,他很可能会先问你几十个问题,然后才开始写第一行代码。AI 同样需要清晰明确的需求。
初学者最常犯的错误,就是问题问得太模糊。让我们看看两者之间的区别:
"Create a button"
✅ 具体的 prompt(好)
"Create a blue button with white text that says 'Submit' and changes
to a darker blue when you hover over it. Use HTML and CSS."
第二个 prompt 效果更好的原因:
不要只是索要代码,还要说明项目的整体背景。
"I'm building a simple to-do list app for beginners. I need a function
that adds a new task to an array and displays it on the webpage.
Use JavaScript and make it easy to understand."
不要羞于承认自己是初学者!AI 可以据此调整解释方式。
"I'm new to JavaScript. Can you create a simple calculator that adds
two numbers? Please explain each line of code so I can learn."
AI 掌握数百种编程语言。你需要告诉它想使用哪一种。
"Create a contact form using HTML for structure, CSS for styling,
and JavaScript for validation. I'm learning web development basics."
只拿到代码却不理解,无非是在复制粘贴。让 AI 教会你其中的原理。
"Create a simple function that checks if a password is strong enough.
Use Python and explain what each part does so I can understand the logic."
准确告诉 AI,你希望它以什么格式回答。
"Give me the HTML and CSS code separately, add comments to explain
each section, and include instructions on how to test it in a browser."
"Make a webpage"
"Create a simple personal webpage for a beginner. Include:
- A header with my name 'John Doe'
- A paragraph about me
- A list of my hobbies
- Basic CSS to make it look nice
- Comments explaining each HTML tag
Use beginner-friendly HTML and CSS only."
"Write a function"
"I'm learning JavaScript functions. Create a simple function that:
- Takes a person's age as input
- Returns whether they can vote (18 or older)
- Include comments explaining how if/else statements work
- Show me how to test the function with different ages"
"This doesn't work, fix it"
"I'm a beginner and this JavaScript code isn't working. Here's my code:
[paste your code]
The error message says: [paste error message]
Can you explain what's wrong and show me the fixed version?
Please explain why the error happened so I can avoid it next time."
复制下面这些模板,再根据你的具体需求填写内容。
"I'm learning [topic/language]. Can you create a simple example of
[specific thing] with comments explaining each part? Make it beginner-friendly."
"I'm building a [type of project] as a beginner project. I need
[specific feature] that does [specific action]. Use [languages]
and include comments explaining the code."
"I'm new to programming and have an error. Here's my code: [code]
The error is: [error message]
Can you explain what's wrong and show me how to fix it?"
"I wrote this [language] code as a beginner. Can you review it and
suggest improvements? Please explain why each suggestion would make
the code better: [your code]"
不要说:“代码运行不了。”
应该说:“我在 Windows 上使用 Chrome 浏览器。打开 HTML 文件后,把鼠标悬停在按钮上时,按钮的颜色没有发生变化。”
不要说:“帮我构建一个完整的社交媒体应用。”
应该说:“先帮我创建一个简单的用户注册表单,之后我们再逐步添加更多功能。”
如果有不理解的地方,就问出来!AI 很有耐心,也不会评判你。
“你能解释一下这一行代码的作用吗?”
“这个报错是什么意思?”
“有没有更简单的实现方式?”
随着你越来越熟悉这种方式,就可以开始与 AI 进行真正的多轮对话:
你:“创建一个简单的 JavaScript 函数,用来计算矩形的面积。”
你:“能不能修改一下,让它也能计算周长?并解释这两种计算背后的数学原理。”
AI:[提供更新后的代码和解释]
你:“如果我希望它适用于任意形状,而不只是矩形,该怎么办?”
这种来回交流的方式,可以让你在构建真正所需内容的同时不断学习。
请记住:每一位专家都曾经是初学者。区别在于,他们学会了如何提出更好的问题。掌握这些 prompt 技巧之后,你得到的不只是代码,还能加速自己的整个学习过程。
部分评论可能只有登录后的访客才能看到。请登录以查看全部评论。
如果需要采取进一步行动,你可以考虑屏蔽此人和/或举报滥用行为。