🐍 Khóa học Lập trình Python cơ bản
Lộ trình học Python từ con số 0 – dành cho học sinh THPT
0 / 20 bài đã học
dict, hàm, vòng lặp, try/except, lưu/đọc dữ liệu.
# Lời giải gợi ý (Bài 20) – ý tưởng
# 1) Dùng dict: diem[ten] = d
# 2) Menu while True + if/elif
# 3) Lưu dữ liệu: join từng dòng "ten|diem"
# 4) Tải dữ liệu: splitlines + split("|")
# 5) Nhập an toàn: try/except
try/except để chương trình không bị dừng khi nhập sai.
# Lời giải gợi ý (Bài 19)
def input_int_pos(msg):
while True:
try:
n = int(input(msg))
if n > 0: return n
except:
pass
print("Nhap sai, nhap lai!")
def input_float(msg):
while True:
try:
return float(input(msg))
except:
print("Nhap sai, nhap lai!")
n = input_int_pos("n=")
ds = [input_float("x=") for _ in range(n)]
print(sum(ds), sum(ds)/n)
# Lời giải gợi ý (Bài 18)
from js import localStorage
KEY = "CB1_B18_FILE"
ds = [1, 2, 3]
localStorage.setItem(KEY, ",".join(map(str, ds)))
txt = localStorage.getItem(KEY)
ds2 = list(map(float, txt.split(",")))
print(ds2)
strip(), split(), lower()
# Lời giải gợi ý (Bài 17)
s = input()
s_chuan = " ".join(s.strip().split())
tu = s_chuan.split()
print(len(tu))
if tu:
tu_dai = max(tu, key=len)
print(tu_dai, len(tu_dai))
k = input().strip().lower()
dem = sum(1 for w in tu if w.lower() == k)
print(dem)
dict, in, items(), values()
# Lời giải gợi ý (Bài 16)
diem = {}
n = int(input("Nhap n: "))
for _ in range(n):
ten = input("Ten: ").strip()
d = float(input("Diem: "))
diem[ten] = d
for ten, d in diem.items():
print(ten, d)
t = input("Tra cuu: ").strip()
print(diem.get(t, "Khong tim thay"))
print("TB =", sum(diem.values())/len(diem))
# Lời giải gợi ý (Bài 15)
def tinh_tong(ds):
return sum(ds)
def dem_so_chan(ds):
return sum(1 for x in ds if x % 2 == 0)
def tim_max(ds):
return max(ds)
n = int(input("Nhap n: "))
ds = [int(input()) for _ in range(n)]
print(sum(ds), dem_so_chan(ds), max(ds))
# Lời giải gợi ý (Bài 14)
def ham_tong(n):
return sum(range(1, n+1))
def la_so_chan(x):
return x % 2 == 0
n = int(input("Nhap n: "))
print("Tong =", ham_tong(n))
x = int(input("Nhap x: "))
if la_so_chan(x):
print("CHAN")
else:
print("LE")
%, for, append, in, index()
# Lời giải gợi ý (Bài 13)
n = int(input("Nhap n: "))
ds = []
for i in range(n):
ds.append(int(input("Nhap so: ")))
dem_chan = sum(1 for v in ds if v % 2 == 0)
dem_le = n - dem_chan
ds_duong = [v for v in ds if v > 0]
x = int(input("Nhap x: "))
if x in ds:
print("Index =", ds.index(x))
else:
print("Khong co")
# Lời giải gợi ý (Bài 12)
n = int(input("Nhap n: "))
ds = []
for i in range(n):
x = float(input("Nhap so: "))
ds.append(x)
print("Danh sach:", ds)
print("Tong =", sum(ds))
print("Max =", max(ds))
print("Min =", min(ds))
print("Trung binh =", sum(ds)/n)
len(), upper(), lower(), in
# Lời giải gợi ý (Bài 11)
s = input("Nhap ho ten: ")
print("Do dai:", len(s))
print("IN HOA:", s.upper())
print("in thuong:", s.lower())
if len(s) >= 3:
print("3 ky tu dau:", s[:3])
if "An" in s:
print("Co 'An'")
else:
print("Khong co 'An'")
📌 Danh sách bình luận