Thanhcong_week3 by cong0999 · Pull Request #154 · CocAgent/python-journey · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions EX1.py
66 changes: 66 additions & 0 deletions EX2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
Bài tập 01: if/elif/else cơ bản 🔀
====================================
Mục tiêu: Viết câu lệnh điều kiện đúng cú pháp
"""

# TODO 1: Nhập tuổi, in ra nhóm tuổi
# < 13: "Thiếu nhi"
# 13-17: "Thiếu niên"
# 18-64: "Người lớn"
# >= 65: "Người cao tuổi"

tuoi = int(input("Nhập tuổi: "))
if tuoi < 13:
print("Thiếu nhi")
elif tuoi <= 17:
print("Thiếu niên")
elif tuoi <= 64:
print("Người lớn")
else:
print("Người cao tuổi")


# TODO 2: Nhập điểm (0-10), xếp loại:
# >= 9: Xuất sắc, >= 8: Giỏi, >= 6.5: Khá, >= 5: TB, < 5: Yếu

diem = float(input("Nhập điểm: "))
if diem >= 9:
print("Xuất sắc")
elif diem >= 8:
print("Giỏi")
elif diem >= 6.5:
print("Khá")
elif diem >= 5:
print("TB")
else:
print("Yếu")


# TODO 3: Nhập năm, kiểm tra năm nhuận
# Năm nhuận: chia hết cho 4, NHƯNG không chia hết cho 100,
# TRỪ KHI chia hết cho 400
# 2000 → nhuận, 1900 → không, 2024 → nhuận

nam = int(input("Nhập năm: "))
if nam % 4 == 0 and (nam % 100 != 0 or nam % 400 == 0):
print(f"{nam} là năm nhuận")
else:
print(f"{nam} không là năm nhuận")


# TODO 4 (Thử thách): Nhập 3 số, in ra số lớn nhất
# KHÔNG dùng hàm max() — chỉ dùng if/elif/else

so1 = float(input("Nhập số thứ 1: "))
so2 = float(input("Nhập số thứ 2: "))
so3 = float(input("Nhập số thứ 3: "))

if so1 >= so2 and so1 >= so3:
lon_nhat = so1
elif so2 >= so1 and so2 >= so3:
lon_nhat = so2
else:
lon_nhat = so3

print(f"Số lớn nhất là: {lon_nhat}")
81 changes: 81 additions & 0 deletions EX4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import random


# ===== Bài 1: Xếp loại học sinh =====
def classify_score(score: float) -> str:
if score < 0 or score > 10:
return "Điểm không hợp lệ (phải từ 0 đến 10)"
if score >= 9:
return "Xuất sắc"
elif score >= 8:
return "Giỏi"
elif score >= 6.5:
return "Khá"
elif score >= 5:
return "Trung bình"
else:
return "Yếu"


# ===== Bài 2: Máy tính 4 phép =====
def calculate(a: float, b: float, op: str):
if op == "+":
return a + b
elif op == "-":
return a - b
elif op == "*":
return a * b
elif op == "/":
if b == 0:
return "Lỗi: không thể chia cho 0"
return a / b
else:
return "Lỗi: phép toán không hợp lệ"


# ===== Bài 3: Năm nhuận =====
def is_leap_year(year: int) -> bool:
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)


# ===== Bài 4: Game đoán số =====
def guessing_game():
secret = random.randint(1, 100)
attempts = 0
print("Tôi đã nghĩ ra một số từ 1 đến 100. Hãy đoán xem!")

while True:
guess = int(input("Đoán số của bạn: "))
attempts += 1
if guess < secret:
print("Thấp hơn rồi, thử lại nhé!")
elif guess > secret:
print("Cao hơn rồi, thử lại nhé!")
else:
print(f"Chính xác! Số đó là {secret}. Bạn đoán đúng sau {attempts} lần.")
break


# ===== Chạy thử (menu chọn bài) =====
if __name__ == "__main__":
print("1. Xếp loại học sinh")
print("2. Máy tính")
print("3. Kiểm tra năm nhuận")
print("4. Game đoán số")
choice = input("Chọn bài (1-4): ")

if choice == "1":
diem = float(input("Nhập điểm (0-10): "))
print(classify_score(diem))
elif choice == "2":
a = float(input("Số thứ nhất: "))
op = input("Phép toán (+, -, *, /): ")
b = float(input("Số thứ hai: "))
print("Kết quả:", calculate(a, b, op))
elif choice == "3":
year = int(input("Nhập năm: "))
print(f"{year} là năm nhuận" if is_leap_year(year) else f"{year} không phải năm nhuận")
elif choice == "4":
guessing_game()
else:
print("Lựa chọn không hợp lệ")
81 changes: 81 additions & 0 deletions cong.py