Python Exercises - TA Version
Bài Tập 1: Tính Lũy Thừa Bằng Vòng Lặp (Calculate Power with Loop)
Đề bài: Nhập hai số a và n. Tính a^n (a mũ n) bằng vòng lặp (không dùng **).
Problem: Enter two numbers a and n. Calculate a^n (a to power n) using loop (no **).
Ví dụ:
Enter a: 2
Enter n: 5Output:
Result: 32Bài Tập 2: Hình Chữ Nhật Ngang (Horizontal Rectangle)
Đề bài: Nhập độ dài n. In n dấu sao trên cùng một dòng.
Problem: Enter length n. Print n stars on the same line.
Ví dụ:
Enter n: 8Output:
* * * * * * * *Bài Tập 3: In 'Hello' n Lần (Print 'Hello' n Times)
Đề bài: Nhập số n. In chữ 'Hello' n lần, mỗi lần trên một dòng.
Problem: Enter number n. Print 'Hello' n times, one per line.
Ví dụ:
Enter n: 4Output:
Hello
Hello
Hello
HelloBài Tập 4: Tính Tích của n Số (Product of n Numbers)
Đề bài: Nhập n số nguyên. Tính và in tích của tất cả các số.
Problem: Enter n integers. Calculate and print the product of all numbers.
Ví dụ:
Enter count: 4
Numbers: 2, 3, 4, 5Output:
Product: 120Bài Tập 5: In Số Với Prefix (Print Numbers with Prefix)
Đề bài: Nhập số n. In các số từ 1 đến n với prefix 'Number: '.
Problem: Enter n. Print numbers from 1 to n with prefix 'Number: '.
Ví dụ:
Enter n: 5Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5Bài Tập 6: Đếm Số Có Hai Chữ Số (Count Two-Digit Numbers)
Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số có hai chữ số (10-99 hoặc -99 đến -10).
Problem: Enter n integers. Count how many have two digits (10-99 or -99 to -10).
Ví dụ:
Enter count: 6
Numbers: 5, 25, 100, -50, 3, 99Output:
Count: 3Bài Tập 7: In Số Trong Khoảng (Print Numbers in Range)
Đề bài: Nhập n số nguyên. Chỉ in các số trong khoảng 10-50 (bao gồm 10 và 50).
Problem: Enter n integers. Print only numbers in range 10-50 (inclusive).
Ví dụ:
Enter count: 6
Numbers: 5, 25, 60, 10, 50, 3Output:
25
10
50Bài Tập 8: Trung Bình Số Dương (Average of Positive Numbers)
Đề bài: Nhập n số nguyên. Tính trung bình CHỈ các số dương. Nếu không có số dương, in 'No positive numbers'.
Problem: Enter n integers. Calculate average of ONLY positive numbers. If no positive numbers, print 'No positive numbers'.
Ví dụ:
Enter count: 5
Numbers: -3, 10, -5, 20, 30Output:
Average: 20.0Bài Tập 9: Tổng Số Chẵn (Sum of Even Numbers)
Đề bài: Nhập n số nguyên. Tính tổng CHỈ các số chẵn.
Problem: Enter n integers. Calculate sum of ONLY even numbers.
Ví dụ:
Enter count: 5
Numbers: 10, 15, 22, 7, 8Output:
Sum: 40Bài Tập 10: Đếm Số Chia Hết cho 2 HOẶC 3 (Count Numbers Divisible by 2 OR 3)
Đề bài: Nhập n số nguyên. Đếm có bao nhiêu số chia hết cho 2 hoặc 3.
Problem: Enter n integers. Count how many are divisible by 2 or 3.
Ví dụ:
Enter count: 5
Numbers: 10, 13, 9, 7, 12Output:
Count: 3Bài Tập 11: Hình Chữ Nhật Rỗng (Hollow Rectangle)
Đề bài: Nhập chiều rộng w và chiều cao h. In hình chữ nhật rỗng bằng dấu sao.
Problem: Enter width w and height h. Print hollow rectangle with stars.
Ví dụ:
Width: 5
Height: 4Output:
* * * * *
* *
* *
* * * * *Bài Tập 12: Bậc Thang Số (Number Staircase)
Đề bài: Nhập số n. In bậc thang số: dòng 1 là '1', dòng 2 là '22', dòng 3 là '333', v.v.
Problem: Enter n. Print number staircase: row 1 is '1', row 2 is '22', row 3 is '333', etc.
Ví dụ:
Enter n: 5Output:
1
22
333
4444
55555Bài Tập 13: Tính Tiền Điện (Electricity Bill Calculator)
Đề bài: Nhập n số kWh. Tính tiền: 0-50 kWh: 1000đ/kWh, 51-100: 1500đ/kWh, >100: 2000đ/kWh.
Problem: Enter n kWh values. Calculate bill: 0-50 kWh: 1000/kWh, 51-100: 1500/kWh, >100: 2000/kWh.
Ví dụ:
Enter count: 3
kWh: 30, 75, 120Output:
30000
67500
170000Bài Tập 14: Xác Thực Mật Khẩu Đơn Giản (Simple Password Validation)
Đề bài: Người dùng có 3 lần thử để nhập đúng mật khẩu. Mật khẩu là 'python123'. In 'Access granted' hoặc 'Access denied'.
Problem: User has 3 attempts to enter correct password. Password is 'python123'. Print 'Access granted' or 'Access denied'.
Ví dụ:
Password: abc
Wrong! Try again
Password: xyz
Wrong! Try again
Password: python123
Access grantedOutput:
Access grantedBài Tập 15: Số May Mắn (Lucky Numbers)
Đề bài: Nhập n số nguyên. In 'Lucky' nếu số chia hết cho 7 HOẶC có chữ số tận cùng là 7, ngược lại in 'Not lucky'.
Problem: Enter n integers. Print 'Lucky' if divisible by 7 OR ends with 7, otherwise print 'Not lucky'.
Ví dụ:
Enter count: 4
Numbers: 14, 17, 25, 7Output:
Lucky
Lucky
Not lucky
LuckyBài Tập 16: Đoán Số Có Giới Hạn (Number Guessing with Limit)
Đề bài: Chương trình nghĩ ra một số từ 1-50 (người dùng nhập số bí mật). Người chơi có tối đa 7 lần đoán. Mỗi lần đoán sai, cho gợi ý 'cao hơn' hoặc 'thấp hơn'. Nếu đoán đúng hoặc hết lượt, kết thúc game.
Problem: Program thinks of a number from 1-50 (user enters secret number). Player has max 7 guesses. For each wrong guess, give hint 'higher' or 'lower'. If guessed correctly or out of attempts, end game.
Ví dụ:
Secret number: 25
Guess: 30
Lower!
Guess: 20
Higher!
Guess: 25
Correct!Output:
Lower!
Higher!
Correct! You won in 3 attempts.Bài Tập 17: Đảo Ngược Chữ Số (Reverse Digits)
Đề bài: Nhập một số nguyên dương. Đảo ngược các chữ số của số đó và in ra.
Problem: Enter a positive integer. Reverse its digits and print.
Ví dụ:
Enter number: 12345Output:
54321Bài Tập 18: Số Palindrome (Palindrome Number)
Đề bài: Nhập một số nguyên dương. Kiểm tra số đó có phải số palindrome không (đọc xuôi ngược giống nhau).
Problem: Enter a positive integer. Check if it's a palindrome number (reads same forwards and backwards).
Ví dụ:
Enter number: 12321Output:
PalindromeBài Tập 19: Dãy Fibonacci (Fibonacci Sequence)
Đề bài: Nhập số n (n > 2). In n số Fibonacci đầu tiên. Fibonacci: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2).
Problem: Enter n (n > 2). Print first n Fibonacci numbers. Fibonacci: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2).
Ví dụ:
Enter n: 8Output:
0 1 1 2 3 5 8 13Bài Tập 20: Hình Kim Cương Số (Number Diamond)
Đề bài: Nhập n (lẻ). In hình kim cương bằng số: nửa trên tăng 1,3,5..., nửa dưới giảm.
Problem: Enter n (odd). Print diamond with numbers: upper half increases 1,3,5..., lower half decreases.
Ví dụ:
Enter n: 5Output:
1
123
12345
123
1ĐÁP ÁN / SOLUTIONS
Solution 1: Tính Lũy Thừa Bằng Vòng Lặp
Giải thích: Nhân a với chính nó n lần.
Explanation: Multiply a by itself n times.
Code:
pythona = int(input("Enter a: ")) n = int(input("Enter n: ")) result = 1 for i in range(n): result *= a print(f"Result: {result}")
Solution 2: Hình Chữ Nhật Ngang
Giải thích: In dấu sao với end=' ' để ở cùng dòng.
Explanation: Print stars with end=' ' to stay on same line.
Code:
pythonn = int(input("Enter n: ")) for i in range(n): print("*", end=" ") print()
Solution 3: In 'Hello' n Lần
Giải thích: Vòng lặp đơn giản in cùng một chuỗi n lần.
Explanation: Simple loop prints same string n times.
Code:
pythonn = int(input("Enter n: ")) for i in range(n): print("Hello")
Solution 4: Tính Tích của n Số
Giải thích: Khởi tạo tích = 1. Mỗi số nhân vào tích.
Explanation: Initialize product = 1. Multiply each number into product.
Code:
pythonn = int(input("Enter count: ")) product = 1 for i in range(n): number = int(input("Enter number: ")) product *= number print(f"Product: {product}")
Solution 5: In Số Với Prefix
Giải thích: In với format string đơn giản.
Explanation: Print with simple format string.
Code:
pythonn = int(input("Enter n: ")) for i in range(1, n + 1): print(f"Number: {i}")
Solution 6: Đếm Số Có Hai Chữ Số
Giải thích: Kiểm tra hai khoảng cho số dương và số âm.
Explanation: Check two ranges for positive and negative numbers.
Code:
pythonn = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if (10 <= number <= 99) or (-99 <= number <= -10): count += 1 print(f"Count: {count}")
Solution 7: In Số Trong Khoảng
Giải thích: Kiểm tra số nằm trong khoảng bằng toán tử so sánh kết hợp.
Explanation: Check if number is in range using combined comparison operators.
Code:
pythonn = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if 10 <= number <= 50: print(number)
Solution 8: Trung Bình Số Dương
Giải thích: Tính tổng và đếm số dương, sau đó chia. Kiểm tra trường hợp đặc biệt.
Explanation: Sum and count positive numbers, then divide. Check edge case.
Code:
pythonn = int(input("Enter count: ")) total = 0 count = 0 for i in range(n): number = int(input("Enter number: ")) if number > 0: total += number count += 1 if count > 0: average = total / count print(f"Average: {average:.1f}") else: print("No positive numbers")
Solution 9: Tổng Số Chẵn
Giải thích: Chỉ cộng số chẵn vào tổng.
Explanation: Only add even numbers to sum.
Code:
pythonn = int(input("Enter count: ")) total = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0: total += number print(f"Sum: {total}")
Solution 10: Đếm Số Chia Hết cho 2 HOẶC 3
Giải thích: Dùng OR để kiểm tra một trong hai điều kiện.
Explanation: Use OR to check one of two conditions.
Code:
pythonn = int(input("Enter count: ")) count = 0 for i in range(n): number = int(input("Enter number: ")) if number % 2 == 0 or number % 3 == 0: count += 1 print(f"Count: {count}")
Solution 11: Hình Chữ Nhật Rỗng
Giải thích: Kiểm tra vị trí để in sao hoặc khoảng trắng.
Explanation: Check position to print star or space.
Code:
pythonw = int(input("Width: ")) h = int(input("Height: ")) for i in range(h): for j in range(w): if i == 0 or i == h - 1 or j == 0 or j == w - 1: print("*", end=" ") else: print(" ", end=" ") print()
Solution 12: Bậc Thang Số
Giải thích: Vòng ngoài cho dòng, vòng trong in số của dòng đó nhiều lần.
Explanation: Outer loop for rows, inner loop prints row number multiple times.
Code:
pythonn = int(input("Enter n: ")) for i in range(1, n + 1): for j in range(i): print(i, end="") print()
Solution 13: Tính Tiền Điện
Giải thích: Tính tiền theo bậc giá lũy tiến.
Explanation: Calculate bill using progressive pricing tiers.
Code:
pythonn = int(input("Enter count: ")) for i in range(n): kwh = int(input("kWh: ")) bill = 0 if kwh <= 50: bill = kwh * 1000 elif kwh <= 100: bill = 50 * 1000 + (kwh - 50) * 1500 else: bill = 50 * 1000 + 50 * 1500 + (kwh - 100) * 2000 print(bill)
Solution 14: Xác Thực Mật Khẩu Đơn Giản
Giải thích: Vòng lặp 3 lần, break khi đúng, kiểm tra trạng thái cuối.
Explanation: Loop 3 times, break when correct, check final status.
Code:
pythoncorrect_password = "python123" granted = False for i in range(3): password = input("Password: ") if password == correct_password: print("Access granted") granted = True break else: if i < 2: print("Wrong! Try again") if not granted: print("Access denied")
Solution 15: Số May Mắn
Giải thích: Kiểm tra hai điều kiện bằng OR.
Explanation: Check two conditions using OR.
Code:
pythonn = int(input("Enter count: ")) for i in range(n): number = int(input("Enter number: ")) if number % 7 == 0 or number % 10 == 7: print("Lucky") else: print("Not lucky")
Solution 16: Đoán Số Có Giới Hạn
Giải thích: Vòng lặp tối đa 7 lần. Mỗi lần so sánh guess với secret, đưa gợi ý. Dùng break để thoát khi đoán đúng.
Explanation: Loop maximum 7 times. Each time compare guess with secret, give hint. Use break to exit when guessed correctly.
Code:
pythonsecret = int(input("Secret number (1-50): ")) max_attempts = 7 attempts = 0 for i in range(max_attempts): guess = int(input("Guess: ")) attempts += 1 if guess == secret: print(f"Correct! You won in {attempts} attempts.") break elif guess > secret: print("Lower!") else: print("Higher!") if attempts == max_attempts: print(f"Game over! The number was {secret}.")
Solution 17: Đảo Ngược Chữ Số
Giải thích: Lấy từng chữ số từ cuối, xây dựng số mới từ đầu.
Explanation: Extract digits from end, build new number from start.
Code:
pythonnum = int(input("Enter number: ")) reversed_num = 0 while num > 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num = num // 10 print(reversed_num)
Solution 18: Số Palindrome
Giải thích: Đảo ngược số rồi so sánh với số gốc.
Explanation: Reverse number then compare with original.
Code:
pythonnum = int(input("Enter number: ")) original = num reversed_num = 0 while num > 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num = num // 10 if original == reversed_num: print("Palindrome") else: print("Not palindrome")
Solution 19: Dãy Fibonacci
Giải thích: Dùng 2 biến để tính số tiếp theo từ 2 số trước.
Explanation: Use 2 variables to calculate next number from previous 2.
Code:
pythonn = int(input("Enter n: ")) a, b = 0, 1 print(a, end=" ") print(b, end=" ") for i in range(2, n): c = a + b print(c, end=" ") a, b = b, c print()
Solution 20: Hình Kim Cương Số
Giải thích: In nửa trên tăng dần, nửa dưới giảm dần, mỗi dòng in số liên tiếp.
Explanation: Print upper half increasing, lower half decreasing, each row prints consecutive numbers.
Code:
pythonn = int(input("Enter n (odd): ")) mid = (n + 1) // 2 # Upper half for i in range(1, mid + 1): for j in range(1, 2 * i): print(j, end="") print() # Lower half for i in range(mid - 1, 0, -1): for j in range(1, 2 * i): print(j, end="") print()