💻 AI Code Activities

กิจกรรม AI เขียนโค้ด

Practice AI-Assisted Coding While Learning English — ฝึกเขียนโค้ดด้วย AI พร้อมเรียนภาษาอังกฤษ

Week 6 · First Conditional · Hands-On Practice

🌱 Tutorial 1: Ask AI to Write Simple Python Scripts
บทเรียนที่ 1: ให้ AI เขียนสคริปต์ Python ง่ายๆ

📝

Write Simple Scripts with AI — เขียนสคริปต์ง่ายๆ กับ AI 🌱 Easy

สิ่งที่คุณจะได้เรียนรู้:

  • วิธีเขียน prompt ให้ AI สร้างโค้ด Python
  • การใช้ตัวแปร (variables) และฟังก์ชัน print
  • การใช้ first conditional ในการอธิบายโปรแกรม
  • การให้ AI อธิบายโค้ดให้เข้าใจง่าย

What You'll Learn:

Ask AI to write simple Python scripts! You'll learn variables, print statements, and how to describe programs using first conditional.

1สร้างโปรแกรมทักทาย — Create a Greeting Program

เริ่มจากโปรแกรมง่ายๆ ที่ถามชื่อผู้ใช้แล้วทักทาย ใช้ first conditional ในการเขียน prompt!

English: Start with a simple program that asks the user's name and greets them. Use first conditional in your prompt!

# Prompt to send to AI — prompt ที่ส่งให้ AI "Write a Python program that asks for the user's name. If the user enters their name, the program will say hello. If the user leaves it blank, the program will say 'Please enter your name.' Add comments in simple English."
AI will generate code like this (ตัวอย่างโค้ดที่ AI จะสร้าง):

# Ask user for their name
name = input("What is your name? ")

# Check if name is empty
if name:
    print(f"Hello, {name}! Welcome!")
else:
    print("Please enter your name.")

2สร้างโปรแกรมคำนวณอายุ — Create an Age Calculator

ลองสร้างโปรแกรมที่คำนวณอายุจากปีเกิด

English: Try creating a program that calculates age from the birth year.

# Prompt for AI "Write a Python program that asks for the user's birth year. If the user enters a valid year, the program will calculate their age. If the user enters an invalid number, the program will show an error message. Use the current year 2026."

3สร้างโปรแกรมแปลงอุณหภูมิ — Create a Temperature Converter

ให้ AI สร้างโปรแกรมแปลงอุณหภูมิจาก Celsius เป็น Fahrenheit

English: Ask AI to create a temperature converter from Celsius to Fahrenheit.

# Prompt for AI "Write a Python program to convert temperature. If the user chooses Celsius to Fahrenheit, the program will convert using the formula. If the user chooses Fahrenheit to Celsius, the program will convert the other way. Show the formula: F = C * 9/5 + 32"

💡 Writing Good Prompts — เคล็ดลับการเขียน Prompt ที่ดี

Thai: ยิ่ง prompt ชัดเจน ยิ่งได้โค้ดที่ดี! ใช้ first conditional เพื่ออธิบายว่า "ถ้าผู้ใช้ทำ X โปรแกรมจะทำ Y"

English: The clearer your prompt, the better the code! Use first conditional to describe: "If the user does X, the program will do Y."

🐛 Tutorial 2: Debug Code with AI — Find and Fix Errors
บทเรียนที่ 2: ดีบักโค้ดด้วย AI — ค้นหาและแก้ไขข้อผิดพลาด

🔍

Debug Code with AI — แก้บั๊กโค้ดด้วย AI 🔍 Medium

สิ่งที่คุณจะได้เรียนรู้:

  • วิธีค้นหา error ในโค้ด Python
  • วิธีให้ AI ช่วยดีบักโค้ด
  • ประเภทข้อผิดพลาด: syntax error, runtime error, logic error
  • การใช้ first conditional อธิบายปัญหาและวิธีแก้

What You'll Learn:

Learn to find and fix errors in Python code with AI help! Understand syntax errors, runtime errors, and logic errors.

1ค้นหา Syntax Error — Find Syntax Errors

Syntax error คือข้อผิดพลาดจากการเขียนโค้ดผิดรูปแบบ ลองส่งโค้ดที่มี error ให้ AI ช่วยหา

English: A syntax error happens when code doesn't follow the rules. Send buggy code to AI and ask it to find the errors.

# This code has bugs! Can you find them? # โค้ดนี้มีบั๊ก! คุณหาเจอไหม? name = input("What is your name? " print("Hello, " + name) age = input("How old are you? ") next_year = age + 1 print("Next year you will be" + next_year)
# Ask AI to debug — ให้ AI ช่วยดีบัก "Please find and fix all the bugs in this Python code. Explain each bug in simple English and Thai. If there is a syntax error, tell me what the correct syntax will be. If there is a logic error, explain what the code will do wrong."
AI will find these bugs (บั๊กที่ AI จะหา):

1. Line 1: Missing closing parenthesis ) in input()
2. Line 4: age is a string, can't add integer 1 — need int(age) + 1
3. Line 5: Can't concatenate string and integer — need str(next_year)

2แก้ Runtime Error — Fix Runtime Errors

Runtime error เกิดขณะโปรแกรมทำงาน ตัวอย่างเช่น การหารด้วยศูนย์

English: Runtime errors happen while the program runs. For example, dividing by zero.

# This code will crash! Why? num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) result = num1 / num2 print(f"Result: {result}")
# Ask AI — ถาม AI "This code crashes when the user enters 0 as the second number. If the user enters zero, the program will crash with a ZeroDivisionError. Please fix this code so that if the user enters zero, the program will show an error message instead of crashing."

3แก้ Logic Error — Fix Logic Errors

Logic error คือเมื่อโค้ดทำงานได้แต่ให้ผลลัพธ์ผิด ยากที่สุดในการหา!

English: A logic error is when the code runs but gives the wrong result. These are the hardest to find!

# This code gives wrong grades! Fix the logic. score = int(input("Enter your score: ")) if score >= 60: print("Grade: D") elif score >= 70: print("Grade: C") elif score >= 80: print("Grade: B") elif score >= 90: print("Grade: A") else: print("Grade: F")
# Ask AI to find the logic error "This grading program gives wrong results. If a student scores 95, the program will say 'Grade: D' instead of 'Grade: A'. Please find the logic error and fix it. Explain why the order of if/elif matters."

💡 Debugging Tip — เคล็ดลับการดีบัก

Thai: เมื่อพบ error ลองอ่านข้อความ error ก่อน! Python จะบอกบรรทัดที่มีปัญหาและประเภท error ถ้ายังไม่เข้าใจ ค่อยส่งให้ AI

English: When you find an error, read the error message first! Python tells you the line number and error type. If you still don't understand, then ask AI.

🏗️ Tutorial 3: Build a Complete Program with AI
บทเรียนที่ 3: สร้างโปรแกรมเต็มรูปแบบด้วย AI

🌟

Build a Quiz App with AI — สร้างแอปถามตอบด้วย AI 🏆 Hard

สิ่งที่คุณจะได้เรียนรู้:

  • วิธีสร้างโปรแกรมเต็มรูปแบบด้วย AI ทีละขั้นตอน
  • การใช้ functions, loops, และ variables ร่วมกัน
  • การใช้ first conditional วางแผนโปรแกรม
  • การให้ AI เพิ่มฟีเจอร์ใหม่ทีละขั้น

What You'll Learn:

Build a complete quiz application step by step with AI! Learn to use functions, loops, and variables together.

1วางแผนโปรแกรม — Plan the Program

ก่อนเขียนโค้ด ใช้ first conditional วางแผนว่าโปรแกรมจะทำอะไร

English: Before coding, use first conditional to plan what the program will do.

# Planning with First Conditional — วางแผนด้วย First Conditional "I want to build a Python quiz app. Here is my plan: 1. If the program starts, it will show a welcome message. 2. If the user starts the quiz, the program will ask 5 questions. 3. If the user answers correctly, the score will increase by 1. 4. If the user answers incorrectly, the program will show the correct answer. 5. If all questions are done, the program will show the final score. 6. If the score is 4 or higher, the program will say 'Excellent!' 7. If the score is below 3, the program will say 'Keep practicing!' Please write this Python program step by step with comments."

2ให้ AI สร้างโค้ดหลัก — Have AI Create the Core Code

ส่ง prompt ที่วางแผนไว้ให้ AI แล้วอ่านโค้ดที่ได้ ทำความเข้าใจแต่ละส่วน

English: Send your plan to AI and read the code carefully. Understand each part.

AI will create something like this (ตัวอย่าง):

def run_quiz():
  questions = [
    {"q": "What does HTML stand for?", "a": "hypertext markup language"},
    {"q": "What language does AI use most?", "a": "python"},
    ...
  ]
  score = 0
  for item in questions:
    answer = input(item["q"] + " ").lower()
    if answer == item["a"]:
      print("Correct!")
      score += 1
    else:
      print(f"Wrong! Answer: {item['a']}")
  print(f"Score: {score}/{len(questions)}")

3เพิ่มฟีเจอร์ — Add Features Step by Step

ให้ AI เพิ่มฟีเจอร์ใหม่ทีละอย่าง

English: Ask AI to add features one at a time.

# Feature 1: Add difficulty levels "If the user chooses 'easy', the program will show simple questions. If the user chooses 'hard', the program will show difficult questions." # Feature 2: Add a timer "If the user takes more than 10 seconds, the program will say 'Time's up!' If the user answers within 5 seconds, the program will give bonus points." # Feature 3: Save high scores "If the score is higher than the previous best, the program will save it as the new high score."

4ทดสอบและปรับปรุง — Test and Improve

ทดสอบโปรแกรมด้วยตัวเอง ลองใส่ค่าที่ไม่คาดคิด แล้วให้ AI ช่วยแก้ปัญหา

English: Test the program yourself. Try unexpected inputs. Ask AI to help fix any issues.

# Ask AI to improve error handling "If the user enters nothing and presses Enter, the program will ask again. If the user types 'quit', the program will end gracefully. If there is any unexpected error, the program will show a friendly message."

📝 Grammar Checkpoint — ตรวจสอบไวยากรณ์

ก่อนจะจบ ทบทวน First Conditional อีกครั้ง:

Structure / โครงสร้าง:

If + present simple, will + base verb

Will + base verb + if + present simple (กลับด้านได้)

Coding Examples / ตัวอย่างเกี่ยวกับโค้ด:

✅ If you write clean code, the team will understand it easily.

✅ The program will crash if you don't handle the error.

✅ If the variable is empty, the function will return an error.

If you will write clean code... (Don't use "will" after "if"!)

Practice — Complete these sentences / เติมประโยค:

1. If you debug the code, the program ___. (will work correctly)

2. If the syntax ___ wrong, the code will not compile. (is)

3. You will save time if you ___ AI to write boilerplate. (use)

4. If the loop ___ forever, the program will freeze. (runs)

Best Practices — แนวปฏิบัติที่ดีสำหรับ AI-Assisted Coding

💡 Tips for AI-Assisted Coding — เคล็ดลับการเขียนโค้ดด้วย AI

  • Understand before copying: If you copy without understanding, you will not learn — อ่านและเข้าใจโค้ดก่อนคัดลอก
  • Start simple: If you start with small programs, you will build confidence — เริ่มจากโปรแกรมเล็กๆ ก่อน
  • Ask AI to explain: "Explain each line" — ขอให้ AI อธิบายทุกบรรทัด
  • Test edge cases: If you test unusual inputs, you will find hidden bugs — ลองใส่ค่าผิดปกติเพื่อหาบั๊ก
  • Iterate step by step: If you add features one at a time, the code will stay organized — เพิ่มฟีเจอร์ทีละอย่าง
  • Use comments: If you add comments, the code will be easier to read later — เขียนคอมเมนต์เสมอ

⚠️ Things to Watch Out For — สิ่งที่ต้องระวัง

  • AI can write buggy code: Always test the code before trusting it — ทดสอบโค้ดเสมอ
  • AI doesn't know your exact needs: Be specific in your prompts — เขียน prompt ให้ชัดเจน
  • Don't skip learning: If you only copy, you will not become a programmer — อย่าแค่ลอก ต้องเรียนรู้ด้วย
  • Security matters: If you put passwords in code, hackers will find them — อย่าใส่รหัสผ่านในโค้ด

🏆 เก่งมาก! You've Done Great!

คุณได้ฝึกเขียนโค้ดด้วย AI และ First Conditional แล้ว!

You have practiced AI-assisted coding and first conditional!

ตอนนี้คุณพร้อมทำแบบทดสอบแล้ว!

If you take the exam now, you will do great!