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

JavaScript: Biến và lệnh inJavaScript: Variables and output

Script tính tuổi và hiển thị lời chàoA script that calculates age and greets

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

  • Khai báo biến với let và constDeclare variables with let and const
  • Gán và thay đổi giá trị biếnAssign and change variable values
  • Dùng console.log và alertUse console.log and alert

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

Script tính tuổi và hiển thị lời chàoA script that calculates age and greets

Nội dung bài họcLesson content

Câu lệnh if/else trong Pythonif/else statements in Python

Python dùng if/elif/else để kiểm tra điều kiện. Lưu ý: dùng dấu hai chấm ":" sau điều kiện và thụt lề (indent) 4 khoảng trắng cho khối lệnh bên trong. Đây là điểm khác biệt với Scratch — Python dùng thụt lề thay vì khối.Python uses if/elif/else to check conditions. Note: use a colon ":" after the condition and indent 4 spaces for the block inside. This differs from Scratch — Python uses indentation instead of blocks.

python
                      
                        tuoi = 18
if tuoi >= 18:
    print("Bạn là người lớn")
elif tuoi >= 13:
    print("Bạn là thanh thiếu niên")
else:
    print("Bạn là trẻ em")
                      
                    

Vòng lặp forThe for loop

Vòng lặp for trong Python dùng range() để lặp n lần. Ví dụ: for i in range(5) sẽ lặp 5 lần, i chạy từ 0 đến 4. For cũng có thể lặp qua danh sách: for ten in ["An", "Bình", "Cường"].The for loop in Python uses range() to repeat n times. For example: for i in range(5) repeats 5 times, i goes from 0 to 4. For can also loop through a list: for name in ["An", "Binh", "Cuong"].

python
                      
                        for i in range(5):
    print("Lần lặp thứ", i)

# In ra 0, 1, 2, 3, 4

# Lặp qua danh sách
ten = ["An", "Bình", "Cường"]
for t in ten:
    print("Xin chào", t)
                      
                    

Vòng lặp whileThe while loop

Vòng lặp while lặp khi điều kiện còn đúng. Khác với for (lặp n lần cố định), while lặp không xác định. CẨN THẬN: nếu điều kiện không bao giờ sai, while sẽ lặp vô hạn → treo máy. Đảm bảo thay đổi biến bên trong vòng lặp.The while loop repeats as long as the condition is true. Unlike for (fixed n times), while loops an indefinite number of times. WARNING: if the condition never becomes false, while will loop infinitely → freezes. Make sure to change a variable inside the loop.

python
                      
                        diem = 0
while diem < 10:
    print("Điểm hiện tại:", diem)
    diem = diem + 1
print("Đã đạt 10 điểm!")
                      
                    

Game đoán sốNumber guessing game

Game đoán số dùng while + if/else. Máy tính chọn số ngẫu nhiên 1-100. Người dùng đoán. Nếu đoán nhỏ hơn → "lớn hơn", lớn hơn → "nhỏ hơn", đúng → "Chúc mừng!". Lặp while cho đến khi đoán đúng.A number guessing game uses while + if/else. The computer picks a random number 1-100. The user guesses. If guess is too low → "higher", too high → "lower", correct → "Congratulations!". Loop while until guessed correctly.

python
                      
                        import random
so_bi_mat = random.randint(1, 100)
doan = 0
while doan != so_bi_mat:
    doan = int(input("Đoán số 1-100: "))
    if doan < so_bi_mat:
        print("Lớn hơn!")
    elif doan > so_bi_mat:
        print("Nhỏ hơn!")
print("Chúc mừng! Đáp án là", so_bi_mat)
                      
                    

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

1Viết chương trình: nhập tuổi, in ra "Được uống bia" nếu >= 18, không thì "Chưa được".Write a program: input age, print "Can drink beer" if >= 18, otherwise "Not yet".

Gợi ýHint

Dùng int(input()) để nhập số. Dùng if/else.Use int(input()) to get a number. Use if/else.

Lời giảiSolution
python
                      
                        tuoi = int(input("Nhập tuổi: "))
if tuoi >= 18:
    print("Được uống bia / Can drink beer")
else:
    print("Chưa được / Not yet")
                      
                    

2Dùng for để in bảng cửu chương 7 (7×1 đến 7×10).Use for to print the 7 times table (7×1 to 7×10).

Gợi ýHint

for i in range(1, 11): print(7, "x", i, "=", 7*i)for i in range(1, 11): print(7, "x", i, "=", 7*i)

Lời giảiSolution
python
                      
                        for i in range(1, 11):
    print(f"7 x {i} = {7 * i}")
                      
                    

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: Sau điều kiện if trong Python cần đặt gì?Q1: What must be placed after an if condition in Python?

Câu 2: for i in range(5) lặp bao nhiêu lần?Q2: How many times does for i in range(5) loop?

Câu 3: Vòng lặp while lặp khi nào?Q3: When does the while loop repeat?

Câu 4: Lệnh nào nhập số từ người dùng trong Python?Q4: Which command gets a number from the user in Python?

Code mẫuSample code

javascript
                // Tiếng Việt / English: Variables
const ten = "Hà Mi";       // name
const namSinh = 2016;      // birth year
const namNay = 2026;       // current year
let tuoi = namNay - namSinh; // age

console.log(`Xin chào ${ten}! / Hello ${ten}!`);
console.log(`Bạn ${tuoi} tuổi. / You are ${tuoi} years old.`);