💻 AI Code Generation — AI เขียนโค้ด

Learn How AI Can Write Code for You — เรียนรู้วิธีที่ AI สามารถเขียนโค้ดให้คุณได้

Week 6 · First Conditional (If...will...) · AI + GitHub Part 2

🎯 AI Code Generation คืออะไร?
What is AI Code Generation?

🇹🇭 ภาษาไทย

AI Code Generation คือการใช้ปัญญาประดิษฐ์ (AI) เพื่อเขียนโค้ดคอมพิวเตอร์โดยอัตโนมัติ คุณเพียงแค่บอก AI ว่าคุณต้องการให้โปรแกรมทำอะไร แล้ว AI จะเขียนโค้ดให้คุณ!


เครื่องมือ AI สำหรับเขียนโค้ดที่มีชื่อเสียง ได้แก่ GitHub Copilot, ChatGPT, Claude และ Gemini


AI Code Generation ช่วยให้คุณเขียนโค้ดได้ เร็วขึ้น, เรียนรู้ภาษาโปรแกรมใหม่ และ แก้ไขบั๊กได้ง่ายขึ้น

🇬🇧 English

AI Code Generation is using artificial intelligence to write computer code automatically. You simply tell the AI what you want your program to do, and the AI writes the code for you!


Famous AI coding tools include GitHub Copilot, ChatGPT, Claude, and Gemini.


AI Code Generation helps you write code faster, learn new programming languages, and fix bugs more easily.

🌟 Fun Facts — ข้อเท็จจริงน่าสนใจ

  • GitHub Copilot writes about 46% of code for developers who use it! — GitHub Copilot เขียนโค้ดให้ประมาณ 46% สำหรับนักพัฒนาที่ใช้มัน!
  • AI can write code in over 20 programming languages including Python, JavaScript, and HTML — AI สามารถเขียนโค้ดได้มากกว่า 20 ภาษาโปรแกรม
  • The first AI code assistant was launched in 2021 — but now millions of developers use AI every day — เครื่องมือ AI เขียนโค้ดตัวแรกเปิดตัวปี 2021 ตอนนี้มีนักพัฒนาหลายล้านคนใช้ AI ทุกวัน
  • If you learn to use AI for coding, you will save hours of work every week! — ถ้าคุณเรียนรู้การใช้ AI เขียนโค้ด คุณจะประหยัดเวลาหลายชั่วโมงต่อสัปดาห์!

🇹🇭 ทำไมต้องเรียน AI Code Generation?

  • ช่วยให้คุณเริ่มเขียนโค้ดได้แม้ไม่มีประสบการณ์
  • เรียนรู้โปรแกรมมิ่งได้เร็วขึ้นเพราะ AI อธิบายโค้ดให้
  • ทักษะสำคัญสำหรับอาชีพในอนาคต
  • ฝึกภาษาอังกฤษไปพร้อมกับเรียนเขียนโค้ด

🇬🇧 Why Learn AI Code Generation?

  • It helps you start coding even without experience
  • Learn programming faster because AI explains the code
  • An essential skill for future careers
  • Practice English while learning to code

🧪 Section Check — ตรวจสอบความเข้าใจ

AI Code Generation Basics — พื้นฐาน AI เขียนโค้ด

0/2

If you ______ a clear prompt, AI will generate better code.

ถ้าคุณ______ prompt ชัดเจน AI จะสร้างโค้ดที่ดีกว่า

write
will write
writing
✅ "write" — First Conditional: If + present simple, ... will + base verb. ใช้ present simple หลัง if เสมอ ห้ามใช้ "will" หลัง if!

What is AI Code Generation?

AI Code Generation คืออะไร?

A tool for deleting code automatically
Using AI to write computer code automatically
A programming language created by AI
✅ AI Code Generation คือการใช้ AI เพื่อเขียนโค้ดคอมพิวเตอร์โดยอัตโนมัติ คุณบอก AI ว่าต้องการอะไร แล้ว AI จะเขียนโค้ดให้!

📚 คำศัพท์สำคัญ
Key Vocabulary

🐛 Debug
ดีบัก / แก้ไขข้อผิดพลาดในโค้ด
To find and fix errors in computer code.
การค้นหาและแก้ไขข้อผิดพลาดในโค้ดคอมพิวเตอร์
🔄 Refactor
รีแฟคเตอร์ / ปรับปรุงโค้ดให้ดีขึ้น
To improve code structure without changing what it does.
การปรับปรุงโครงสร้างโค้ดโดยไม่เปลี่ยนสิ่งที่มันทำ
📝 Syntax
ซินแทกซ์ / ไวยากรณ์ของภาษาโปรแกรม
The rules for writing code in a programming language — like grammar for code.
กฎสำหรับการเขียนโค้ดในภาษาโปรแกรม เหมือนไวยากรณ์ของโค้ด
⚙️ Function
ฟังก์ชัน / ชุดคำสั่งที่ทำงานเฉพาะ
A reusable block of code that performs a specific task.
ชุดโค้ดที่ใช้ซ้ำได้ ทำหน้าที่เฉพาะอย่าง
📦 Variable
ตัวแปร / ที่เก็บข้อมูลในโค้ด
A container that stores data in a program. Like a box with a label.
ที่เก็บข้อมูลในโปรแกรม เหมือนกล่องที่มีป้ายชื่อ
🔁 Loop
ลูป / การทำซ้ำ
A piece of code that repeats the same action multiple times.
โค้ดที่ทำสิ่งเดิมซ้ำหลายครั้ง
🧮 Algorithm
อัลกอริทึม / ขั้นตอนวิธี
A step-by-step set of instructions to solve a problem.
ชุดคำสั่งทีละขั้นตอนเพื่อแก้ปัญหา
🔨 Compile
คอมไพล์ / แปลงโค้ดเป็นโปรแกรม
To convert code into a program that a computer can run.
การแปลงโค้ดให้เป็นโปรแกรมที่คอมพิวเตอร์รันได้
▶️ Runtime
รันไทม์ / ช่วงเวลาที่โปรแกรมทำงาน
The period when a program is running. Runtime errors happen while the program executes.
ช่วงเวลาที่โปรแกรมกำลังทำงาน ข้อผิดพลาดรันไทม์เกิดขณะโปรแกรมทำงาน
❌ Error
ข้อผิดพลาด / เออเรอร์
A mistake in code that prevents the program from working correctly.
ความผิดพลาดในโค้ดที่ทำให้โปรแกรมทำงานไม่ถูกต้อง
✂️ Snippet
สนิปเพ็ต / ชิ้นส่วนโค้ดสั้นๆ
A small piece of reusable code. AI can generate snippets quickly.
ชิ้นส่วนโค้ดเล็กๆ ที่ใช้ซ้ำได้ AI สร้าง snippet ได้อย่างรวดเร็ว
📄 Boilerplate
บอยเลอร์เพลท / โค้ดพื้นฐานที่ใช้ซ้ำ
Standard code that is used as a starting template for new projects.
โค้ดมาตรฐานที่ใช้เป็นแม่แบบเริ่มต้นสำหรับโปรเจกต์ใหม่

🧪 Section Check — ตรวจสอบความเข้าใจ

Coding Vocabulary — คำศัพท์โค้ดดิ้ง

0/3

If a variable ______ a clear name, the code will be easier to read.

ถ้าตัวแปร______ชื่อที่ชัดเจน โค้ดจะอ่านง่ายขึ้น

has
will have
having
✅ "has" — First Conditional: If + present simple (has), ... will + base verb. หลัง if ใช้ present simple เสมอ!

What does "debug" mean?

"debug" หมายถึงอะไร?

To add new features to a program
To find and fix errors in computer code
To delete a program completely
✅ Debug = การค้นหาและแก้ไขข้อผิดพลาดในโค้ด If you debug the code, the program will work correctly!

A ______ is a reusable block of code that performs a specific task.

______ คือชุดโค้ดที่ใช้ซ้ำได้ ทำหน้าที่เฉพาะอย่าง

variable
loop
function
✅ A function is a reusable block of code. Variable = ที่เก็บข้อมูล, Loop = การทำซ้ำ, Function = ชุดคำสั่งที่ทำงานเฉพาะ

📖 ไวยากรณ์ในบริบท AI Code
Grammar in Context: First Conditional

🔤 First Conditional: If + present simple, will + base verb

First Conditional ใช้พูดถึงสถานการณ์ที่เป็นไปได้ในอนาคต และผลลัพธ์ที่น่าจะเกิดขึ้น เหมาะกับการพูดถึง "ถ้าทำสิ่งนี้ แล้วจะเกิดอะไร"

รูปแบบ: If + subject + present simple, subject + will + base verb

หรือกลับด้าน: Subject + will + base verb + if + subject + present simple

สำคัญ: ใช้ present simple หลัง if (ไม่ใช่ will!) และใช้ will ในส่วนผลลัพธ์

✅ If you write a clear prompt, AI will generate better code.

ถ้าคุณเขียน prompt ชัดเจน AI จะสร้างโค้ดที่ดีกว่า

✅ If the code has a bug, the program will crash.

ถ้าโค้ดมีบั๊ก โปรแกรมจะล่ม

✅ You will save time if you use AI to write boilerplate code.

คุณจะประหยัดเวลาถ้าใช้ AI เขียนโค้ดพื้นฐาน

✅ If you debug the error, the function will work correctly.

ถ้าคุณดีบักข้อผิดพลาด ฟังก์ชันจะทำงานถูกต้อง

❌ If you will write a prompt, AI will generate code.✅ If you write a prompt, AI will generate code.

อย่าใช้ "will" หลัง if! ใช้ present simple เท่านั้น

💡 Grammar Tip — เคล็ดลับไวยากรณ์

Thai: First Conditional ต่างจาก Zero Conditional ตรงที่ใช้ will ในส่วนผลลัพธ์ เพราะพูดถึงสิ่งที่ น่าจะเกิดขึ้น ในอนาคต ไม่ใช่ความจริงทั่วไป

English: The first conditional uses will for the result because it talks about things that are likely to happen in the future, not general truths.

🇹🇭 ตัวอย่าง First Conditional กับ AI Coding

  • If you learn Python, you will be able to automate tasks. — ถ้าเรียน Python จะทำให้งานอัตโนมัติได้
  • If the syntax is wrong, the code will not compile. — ถ้าไวยากรณ์โค้ดผิด โค้ดจะคอมไพล์ไม่ได้
  • If you refactor the code, it will run faster. — ถ้ารีแฟคเตอร์โค้ด มันจะรันเร็วขึ้น
  • If you use a loop, the program will repeat the action. — ถ้าใช้ลูป โปรแกรมจะทำซ้ำ
  • If you don't debug, the error will remain. — ถ้าไม่ดีบัก ข้อผิดพลาดจะยังคงอยู่

🇬🇧 First Conditional Examples with AI Coding

  • If you learn Python, you will be able to automate tasks.
  • If the syntax is wrong, the code will not compile.
  • If you refactor the code, it will run faster.
  • If you use a loop, the program will repeat the action.
  • If you don't debug, the error will remain.

🧪 Section Check — ตรวจสอบความเข้าใจ

First Conditional Grammar — ไวยากรณ์ First Conditional

0/3

If the syntax ______ wrong, the code will not compile.

ถ้าไวยากรณ์โค้ด______ โค้ดจะคอมไพล์ไม่ได้

is
will be
being
✅ "is" — First Conditional: If + present simple, will + base verb. หลัง if ใช้ present simple (is) ไม่ใช่ "will be"!

Which sentence uses the first conditional CORRECTLY?

ประโยคไหนใช้ first conditional ถูกต้อง?

If you will use a loop, the program repeats.
If you will refactor, it will run faster.
If you use a loop, the program will repeat the action.
✅ "If you use a loop, the program will repeat the action." — ถูกต้อง! If + present simple (use), will + base verb (repeat). อย่าใช้ "will" หลัง if

You ______ time if you use AI to write boilerplate code.

คุณ______เวลาถ้าใช้ AI เขียนโค้ดพื้นฐาน

save
will save
saving
✅ "will save" — เมื่อกลับด้านประโยค ส่วนผลลัพธ์ (result clause) ใช้ will + base verb ส่วน if clause ยังคงใช้ present simple (use)

🚀 เริ่มต้นใช้ AI เขียนโค้ด Python
Getting Started: Using AI to Write Python Code

🇹🇭 สิ่งที่ต้องเตรียม

  • คอมพิวเตอร์หรือโทรศัพท์ที่เชื่อมต่ออินเทอร์เน็ต
  • บัญชี ChatGPT หรือ Claude (ฟรี)
  • ความกระตือรือร้นที่จะลองเขียนโค้ด!

🇬🇧 What You Need

  • A computer or phone with internet access
  • A ChatGPT or Claude account (free)
  • Enthusiasm to try coding!

1เปิด AI Chatbot — Open an AI Chatbot

ไปที่ chat.openai.com (ChatGPT) หรือ claude.ai (Claude) แล้วล็อกอินเข้าบัญชีของคุณ
English: Go to chat.openai.com (ChatGPT) or claude.ai (Claude) and log in to your account.

2บอก AI ว่าคุณต้องการอะไร — Tell AI What You Want

เขียน prompt ที่ชัดเจนเป็นภาษาอังกฤษ บอก AI ว่าคุณต้องการโปรแกรมอะไร ใช้ภาษาที่ง่ายและเจาะจง
English: Write a clear prompt in English. Tell AI exactly what program you want. Use simple and specific language.
# Example prompt — ตัวอย่าง prompt "Please write a Python program that asks the user for their name and says hello to them."

3อ่านโค้ดที่ AI สร้าง — Read the AI-Generated Code

อย่ารีบคัดลอกโค้ด! อ่านโค้ดที่ AI สร้างให้ก่อน ลองทำความเข้าใจว่าแต่ละบรรทัดทำอะไร
English: Don't just copy the code! Read the AI-generated code first. Try to understand what each line does.
# AI might generate this code: name = input("What is your name? ") print(f"Hello, {name}! Nice to meet you!")

4ขอให้ AI อธิบายโค้ด — Ask AI to Explain the Code

ถ้าไม่เข้าใจโค้ด ให้ถาม AI! พิมพ์ "Please explain each line of this code in simple English and Thai."
English: If you don't understand the code, ask AI! Type "Please explain each line of this code in simple English and Thai."
# Ask AI to explain "Please explain each line of this code in simple English. Also show the Thai translation."

5ลองแก้ไขโค้ด — Try Modifying the Code

ลองขอให้ AI แก้ไขหรือเพิ่มฟีเจอร์ใหม่! นี่คือวิธีที่คุณจะเรียนรู้โปรแกรมมิ่งจริงๆ
English: Try asking AI to modify or add new features! This is how you will really learn programming.
# Modify the code — ลองแก้ไขโค้ด "Please modify this program so it also asks for the user's age and tells them how old they will be next year."

💡 Pro Tip — เคล็ดลับมือโปร

Thai: ใช้ first conditional เมื่อบอก AI ว่าต้องการให้โปรแกรมทำอะไร! เช่น "If the user enters a number, the program will calculate the result."

English: Use first conditional when telling AI what you want the program to do! For example: "If the user enters a number, the program will calculate the result."

🧪 Section Check — ตรวจสอบความเข้าใจ

Getting Started with AI Coding — เริ่มต้นเขียนโค้ดกับ AI

0/3

If you ______ AI to explain the code, you will understand it better.

ถ้าคุณ______ AI ให้อธิบายโค้ด คุณจะเข้าใจมันดีขึ้น

ask
will ask
asked
✅ "ask" — First Conditional: If + present simple (ask), ... will + base verb. หลัง if ใช้ present simple เสมอ ไม่ใช้ will หรือ past tense!

What is the correct order when using AI to write code?

ลำดับที่ถูกต้องเมื่อใช้ AI เขียนโค้ดคืออะไร?

Copy the code → Run it → Never look at it again
Write code yourself → Ask AI to delete it → Start over
Write a prompt → Read the code → Ask AI to explain → Modify it
✅ ขั้นตอนที่ถูกต้อง: เขียน prompt → อ่านโค้ด → ขอ AI อธิบาย → ลองแก้ไข If you follow these steps, you will learn programming effectively!

If you ______ a specific prompt, the AI will give a better answer.

ถ้าคุณ______prompt ที่เจาะจง AI จะให้คำตอบที่ดีกว่า

will write
write
writing
✅ "write" — First Conditional: If + present simple (write), ... will + base verb (give). ห้ามใช้ "will" หรือ "-ing" หลัง if!

🛠️ โปรเจกต์ลงมือทำ
Hands-On Project: AI Calculator

🧮

Build a Calculator with AI — สร้างโปรแกรมเครื่องคิดเลขด้วย AI 🌱 Beginner

เป้าหมาย: ใช้ AI สร้างโปรแกรมเครื่องคิดเลขด้วย Python ที่สามารถบวก ลบ คูณ หาร ได้

ใช้ first conditional ในการเขียน prompt!

Goal: Use AI to create a Python calculator program that can add, subtract, multiply, and divide.

Use first conditional in your prompts!

# Step 1: Ask AI to build the calculator "Please write a Python calculator program. If the user chooses addition, the program will add two numbers. If the user chooses subtraction, the program will subtract. If the user chooses multiplication, the program will multiply. If the user chooses division, the program will divide. If the user enters zero for division, the program will show an error." # Step 2: Ask AI to explain the code "Please explain how this calculator works, line by line." # Step 3: Ask AI to add features "If the user wants to continue, the program will ask for another calculation. If the user types 'quit', the program will stop."

⚠️ Remember / จำไว้นะ

Thai: AI สร้างโค้ดได้เร็ว แต่คุณต้องเข้าใจว่าโค้ดทำงานอย่างไร! ถ้าคุณเพียงแค่คัดลอก คุณจะไม่ได้เรียนรู้อะไรเลย

English: AI generates code fast, but you need to understand how it works! If you just copy, you will not learn anything.

🧪 Section Check — ตรวจสอบความเข้าใจ

Coding with AI — เขียนโค้ดกับ AI

0/2

If you just copy AI-generated code without reading it, you ______ anything.

ถ้าคุณแค่คัดลอกโค้ดจาก AI โดยไม่อ่าน คุณ______อะไรเลย

don't learn
will not learn
not learning
✅ "will not learn" — First Conditional ใช้ will ในส่วนผลลัพธ์: If you just copy (present simple), you will not learn (will + base verb)

What is the BEST first step when AI generates code for you?

ขั้นตอนแรกที่ดีที่สุดเมื่อ AI สร้างโค้ดให้คุณคืออะไร?

Copy and paste it immediately.
Read the code and try to understand what each line does.
Delete it and write your own from scratch.
✅ อ่านโค้ดก่อนแล้วลองทำความเข้าใจ! If you read and understand the code, you will learn programming faster.

📊 ตารางอ้างอิงคำศัพท์โค้ด
Coding Terms Reference Table

Term / คำศัพท์ Thai / ภาษาไทย Example Sentence (First Conditional)
Debug ดีบัก / แก้ไขข้อผิดพลาด If you debug the code, the program will work.
Refactor รีแฟคเตอร์ / ปรับปรุงโค้ด If you refactor this function, it will be cleaner.
Syntax ไวยากรณ์โค้ด If the syntax is correct, the code will compile.
Function ฟังก์ชัน If you create a function, you will reuse it easily.
Variable ตัวแปร If you name variables clearly, the code will be readable.
Loop ลูป / การทำซ้ำ If you use a loop, the program will repeat the task.
Algorithm อัลกอริทึม If you follow the algorithm, you will solve the problem.
Compile คอมไพล์ If the code compiles, it will be ready to run.
Runtime รันไทม์ If a runtime error occurs, the program will stop.
Error ข้อผิดพลาด If there is an error, the output will be wrong.
Snippet สนิปเพ็ต / โค้ดสั้นๆ If you save code snippets, you will code faster.
Boilerplate โค้ดพื้นฐาน If you use boilerplate, you will start projects quickly.

🧪 Section Check — ตรวจสอบความเข้าใจ

Coding Terms Review — ทบทวนคำศัพท์โค้ดดิ้ง

0/3

If a runtime error ______, the program will stop working.

ถ้าเกิดข้อผิดพลาดรันไทม์______ โปรแกรมจะหยุดทำงาน

occurs
will occur
occurring
✅ "occurs" — First Conditional: If + present simple (occurs), will + base verb (stop). Runtime error = ข้อผิดพลาดที่เกิดขณะโปรแกรมทำงาน

What is a "snippet"?

"snippet" คืออะไร?

A small piece of reusable code
A type of programming language
An error message from the compiler
✅ Snippet = ชิ้นส่วนโค้ดเล็กๆ ที่ใช้ซ้ำได้ If you save useful snippets, you will code faster next time!

If you ______ the code, it will run faster and be easier to read.

ถ้าคุณ______โค้ด มันจะรันเร็วขึ้นและอ่านง่ายขึ้น

compile
refactor
delete
✅ "refactor" — Refactor คือการปรับปรุงโครงสร้างโค้ดให้ดีขึ้นโดยไม่เปลี่ยนสิ่งที่มันทำ If you refactor, the code will be cleaner!

🎯 สรุป
Summary

🇹🇭 สิ่งที่คุณได้เรียนรู้

  • AI Code Generation คืออะไรและทำงานอย่างไร
  • คำศัพท์สำคัญ: debug, refactor, syntax, function, variable, loop, algorithm, compile, runtime, error, snippet, boilerplate
  • First Conditional: If + present simple, will + base verb
  • วิธีใช้ AI เขียนโค้ด Python 5 ขั้นตอน
  • โปรเจกต์: สร้างเครื่องคิดเลขด้วย AI

🇬🇧 What You Have Learned

  • What AI Code Generation is and how it works
  • Key vocabulary: debug, refactor, syntax, function, variable, loop, algorithm, compile, runtime, error, snippet, boilerplate
  • First Conditional: If + present simple, will + base verb
  • How to use AI to write Python code in 5 steps
  • Project: Building a calculator with AI

🧪 Section Check — ตรวจสอบความเข้าใจ

Final Review — ทบทวนสุดท้าย

0/2

If you ______ AI coding tools every day, you will improve quickly.

ถ้าคุณ______เครื่องมือ AI เขียนโค้ดทุกวัน คุณจะพัฒนาเร็วขึ้น

will practice
practice
practiced
✅ "practice" — First Conditional: If + present simple (practice), will + base verb (improve). หลัง if ใช้ present simple เสมอ!

What should you ALWAYS do after AI generates code for you?

คุณควรทำอะไร "เสมอ" หลังจาก AI สร้างโค้ดให้?

Submit it without checking
Test and review the code to make sure it works
Delete everything and start from scratch
✅ ต้องทดสอบและตรวจสอบโค้ดเสมอ! If you test the code carefully, you will catch errors before they cause problems.

🎉 ยินดีด้วย! / Congratulations!

คุณได้เริ่มต้นเขียนโค้ดด้วย AI แล้ว!

If you keep practicing, you will become a great programmer! 💻