专项练习 — 程序填空题
练习说明
- 共 10 道程序填空题,难度与模拟考试相当
- 每题包含 2~3 个填空,标记为
#**********begin********** 和 #**********end**********
- 建议每题用时 3~5 分钟
📝 程序填空题
第1题:while循环求累加和
题目: 使用while循环计算1到100的累加和,并输出结果。
| Python |
|---|
| total = 0
i = 1
#**********begin1**********
_______
#**********end1**********
total += i
#**********begin2**********
_______
#**********end2**********
print("1到100的累加和为:", total)
|
第2题:字符串大小写统计
题目: 统计字符串中大写字母、小写字母和其他字符的个数。
| Python |
|---|
| s = "Hello World! 2024"
upper_count = 0
lower_count = 0
other_count = 0
for ch in s:
#**********begin1**********
_______
#**********end1**********
upper_count += 1
#**********begin2**********
_______
#**********end2**********
lower_count += 1
else:
other_count += 1
print("大写字母:", upper_count)
print("小写字母:", lower_count)
print("其他字符:", other_count)
|
第3题:列表元素筛选
题目: 从列表中筛选出所有正数,并存入新列表输出。
| Python |
|---|
| nums = [-3, 5, -1, 8, 0, 12, -7, 4]
#**********begin1**********
_______
#**********end1**********
for n in nums:
#**********begin2**********
_______
#**********end2**********
positives.append(n)
print("正数列表:", positives)
|
第4题:字典统计成绩等级
题目: 根据成绩列表统计各等级人数:90及以上为A,8089为B,7079为C,60~69为D,60以下为E。
| Python |
|---|
| scores = [95, 82, 73, 65, 55, 88, 91, 47, 78, 60]
grades = {}
for score in scores:
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'E'
#**********begin1**********
_______
#**********end1**********
#**********begin2**********
_______
#**********end2**********
print(f"等级{g}: {grades[g]}人")
|
第5题:函数判断素数
题目: 编写函数判断一个数是否为素数,在主程序中调用并输出结果。
| Python |
|---|
| def is_prime(n):
if n < 2:
return False
#**********begin1**********
_______
#**********end1**********
if n % i == 0:
#**********begin2**********
_______
#**********end2**********
return True
num = 17
if is_prime(num):
print(f"{num}是素数")
else:
print(f"{num}不是素数")
|
第6题:文件行数统计
题目: 读取文本文件,统计文件的行数和非空行数。
| Python |
|---|
| total_lines = 0
non_empty_lines = 0
#**********begin1**********
_______
#**********end1**********
for line in f:
total_lines += 1
#**********begin2**********
_______
#**********end2**********
non_empty_lines += 1
print("总行数:", total_lines)
print("非空行数:", non_empty_lines)
|
第7题:回文字符串判断
题目: 判断一个字符串是否为回文字符串(正读和反读相同)。
| Python |
|---|
| def is_palindrome(s):
left = 0
right = len(s) - 1
#**********begin1**********
_______
#**********end1**********
if s[left] != s[right]:
#**********begin2**********
_______
#**********end2**********
left += 1
right -= 1
return True
text = "level"
if is_palindrome(text):
print(f'"{text}"是回文字符串')
else:
print(f'"{text}"不是回文字符串')
|
第8题:九九乘法表
题目: 使用嵌套循环输出九九乘法表。
| Python |
|---|
| #**********begin1**********
_______
#**********end1**********
#**********begin2**********
_______
#**********end2**********
print(f"{i}*{j}={i*j}", end="\t")
#**********begin3**********
_______
#**********end3**********
|
第9题:冒泡排序基础版
题目: 使用冒泡排序对列表进行升序排列。
| Python |
|---|
| lst = [64, 34, 25, 12, 22, 11, 90]
n = len(lst)
for i in range(n):
for j in range(0, n - i - 1):
#**********begin1**********
_______
#**********end1**********
#**********begin2**********
_______
#**********end2**********
print("排序后:", lst)
|
第10题:文件读取与平均分计算
题目: 从文件中读取学生成绩(每行一个分数),计算平均分、最高分和最低分。
| Python |
|---|
| scores = []
#**********begin1**********
_______
#**********end1**********
for line in f:
score = float(line.strip())
#**********begin2**********
_______
#**********end2**********
if scores:
avg = sum(scores) / len(scores)
#**********begin3**********
_______
#**********end3**********
print(f"平均分: {avg:.1f}, 最高分: {max_score}, 最低分: {min_score}")
|