Cấp độ: elementary30 phútminMiễn phíFreeJunior BuilderJunior Builder

JavaScript: if/else — Game đoán sốJavaScript: if/else — Number guessing game

Game đoán số ngẫu nhiênA random number guessing game

Bạn sẽ học gìWhat you will learn

  • Viết câu lệnh if/else/else ifWrite if/else/else if statements
  • Nhận input từ người dùngGet user input
  • Tạo số ngẫu nhiên với Math.random()Generate random numbers with Math.random()

Bạn sẽ tạo gìWhat you will build

Game đoán số ngẫu nhiênA random number guessing game

Nội dung bài họcLesson content

Hàm (function) là gì?What is a function?

Hàm là khối code có tên, thực hiện một việc cụ thể, có thể gọi lại nhiều lần. Thay vì viết cùng code 10 lần, bạn viết 1 hàm rồi gọi 10 lần. Hàm giúp code gọn, dễ sửa, dễ đọc. Dùng def để tạo hàm.A function is a named block of code that performs a specific task and can be called multiple times. Instead of writing the same code 10 times, you write 1 function and call it 10 times. Functions make code compact, easy to fix, and readable. Use def to create a function.

Tạo hàm với defCreating functions with def

Dùng def ten_ham(): để tạo hàm. Code bên trong thụt lề 4 khoảng trắng. Gọi hàm bằng ten_ham(). Hàm có thể nhận tham số (parameter) và trả về giá trị (return).Use def function_name(): to create a function. Code inside is indented 4 spaces. Call the function with function_name(). Functions can take parameters and return values.

python
                      
                        def chao(ten):
    print("Xin chào", ten)

chao("An")      # In: Xin chào An
chao("Bình")    # In: Xin chào Bình

# Hàm có return
def cong(a, b):
    return a + b

tong = cong(5, 3)
print(tong)     # In: 8
                      
                    

Module: thư viện có sẵnModules: built-in libraries

Module là thư viện code có sẵn mà bạn có thể nhập (import) vào chương trình. Python có hàng tràn module: random (số ngẫu nhiên), math (toán học), time (thời gian), turtle (đồ họa). Dùng import ten_module để nhập.A module is a pre-written code library that you can import into your program. Python has hundreds of modules: random (random numbers), math (mathematics), time (timing), turtle (graphics). Use import module_name to import.

python
                      
                        import random
import math

# Random
so = random.randint(1, 100)
print("Số ngẫu nhiên:", so)

# Math
can_bac_2 = math.sqrt(144)
print("Căn bậc 2 của 144:", can_bac_2)  # 12.0
                      
                    

Module turtle: vẽ đồ họaThe turtle module: graphics

turtle là module đồ họa dành cho người mới bắt đầu. Một "con rùa" di chuyển trên màn hình và vẽ đường. Dùng turtle.forward(100) để đi 100 bước, turtle.right(90) để xoay 90 độ. Kết hợp với for để vẽ hình.turtle is a graphics module for beginners. A "turtle" moves on the screen and draws a trail. Use turtle.forward(100) to move 100 steps, turtle.right(90) to turn 90 degrees. Combine with for to draw shapes.

python
                      
                        import turtle

t = turtle.Turtle()
t.speed(5)

for i in range(4):
    t.forward(100)
    t.right(90)

turtle.done()  # Mở cửa sổ đồ họa
                      
                    

Bài tập thực hànhHands-on exercises

1Viết hàm tinh_tong(a, b) trả về tổng 2 số. Gọi hàm với 10 và 25, in kết quả.Write a function tinh_tong(a, b) that returns the sum of 2 numbers. Call it with 10 and 25, print the result.

Gợi ýHint

def tinh_tong(a, b): return a + bdef tinh_tong(a, b): return a + b

Lời giảiSolution
python
                      
                        def tinh_tong(a, b):
    return a + b

ket_qua = tinh_tong(10, 25)
print("Tổng:", ket_qua)  # 35
                      
                    

2Dùng turtle để vẽ hình lục giác (6 cạnh, xoay 60 độ mỗi lần).Use turtle to draw a hexagon (6 sides, turn 60 degrees each time).

Gợi ýHint

for i in range(6): t.forward(100); t.right(60)for i in range(6): t.forward(100); t.right(60)

Lời giảiSolution
python
                      
                        import turtle
t = turtle.Turtle()
for i in range(6):
    t.forward(100)
    t.right(60)
turtle.done()
                      
                    

Kiểm traQuiz

Chọn đáp án đúng cho mỗi câu. Nhấn để xem giải thích.Choose the correct answer for each question. Click to see explanation.

Câu 1: Từ khóa nào tạo hàm trong Python?Q1: Which keyword creates a function in Python?

Câu 2: Lệnh return trong hàm làm gì?Q2: What does the return statement do in a function?

Câu 3: Lệnh nào nhập module random vào Python?Q3: Which command imports the random module in Python?

Code mẫuSample code

javascript
                const bíMật = Math.floor(Math.random() * 10) + 1; // 1-10
const đoán = parseInt(prompt("Đoán số từ 1-10 / Guess 1-10:"));

if (đoán === bíMật) {
  alert("🎉 Chính xác! / Correct!");
} else if (đoán > bíMật) {
  alert("📉 Quá cao! / Too high!");
} else {
  alert("📈 Quá thấp! / Too low!");
}