首页 › 试卷 › Python编程考级 › Python编程六级 › 青少年软件编程(python六级)等级考试试卷-编程题(2024年6月)

青少年软件编程(python六级)等级考试试卷-编程题(2024年6月) VIP

Python编程六级 共 3 题 2024 年 含标准答案与解析
返回试卷列表
本页为整卷浏览:先自己做一遍,再展开「查看答案与解析」核对。

一、简答题

1. 简答题

运动时长统计

汪龙对不同年龄段的人群进行运动时长的调研,数据存储在文件“sport_240622.csv”中,数据内容如下图显示:

用Python编写一段代码帮他完成本次调用的运动时长的统计。具体要求如下:

(1)读取文件sport_240622.csv中的姓名、年龄、运动时长;

(2)输出年龄在40岁以上(含40岁)人群的平均运动时长(保留2位小数);

请根据要求,补全代码。

import csv
with open("/data/sport_240622.csv") as f:    
    rows = list(        ①        )    
    s=0
    c=0
    pj=0    
    for row in rows[1:]:
        if(        ②        ):        
            s=s+        ③           
                   ④        
    pj=s/c    
    print(        ⑤        )
查看答案与解析
答案:<pre class="brush:python;toolbar:false">import csv with open("/data/sport_240622.csv") as f:         rows = list(csv.reader(f))         s=0     c=0     pj=0         for row in rows[1:]:         if(int(row[1])&gt;=40):                     s=s+int(row[2])             c=c+1          pj=s/c         print(round(pj,2))</pre>
解析:

评分标准:

(1)csv.reader(f) 或等效答案;(2分)

(2)int(row[1])>40 或等效答案;(2分)

(3)int(row[2]) 或等效答案;(2分)

(4)c=c+1 或等效答案;(2分)

(5)round(pj,2) 或等效答案。(2分)

2. 简答题

创建学生库

使用Python的sqlite3库完成以下问题。

(1)创建一个名为students的数据库;

(2)在这个数据库中,创建一个名为students_table的表,包含以下字段:id(主键),name(学生的名字),age(学生的年龄),grade(学生的年级);

(3)向students_table中插入至少5个学生的数据;

(4)查询年龄大于18岁的所有学生,并打印结果;

(5)将名字为"Alice"的学生的年龄增加1岁;

(6)删除名字为"Bob"的学生。

(本题无需运行通过,写入代码即可)

import sqlite3
conn = sqlite3.connect('        ①        ')
cursor = conn.cursor()
cursor.execute('''        ②         students_table(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER,grade TEXT)''')
students = [ ('Alice', 17, '10th'), ('Bob', 18, '11th'), ('Charlie', 16, '10th'),  ('David', 19, '12th'),('Eve', 17, '11th')]
cursor.executemany('''INSERT INTO students_table (name, age, grade) VALUES (?, ?, ?)''', students)
conn.commit()
cursor.execute('SELECT * FROM students_table         ③        ')
print("年龄大于18岁的学生:")
print(cursor.        ④        )
cursor.execute('UPDATE students_table SET age = age + 1 WHERE name = "Alice"')
cursor.execute('DELETE FROM students_table WHERE name = "Bob"')
conn.commit()
conn.close()
查看答案与解析
答案:<pre class="brush:python;toolbar:false">import sqlite3 conn = sqlite3.connect('students.db') cursor = conn.cursor() cursor.execute('''CREATE TABLE students_table(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER,grade TEXT)''') students = [ ('Alice', 17, '10th'), ('Bob', 18, '11th'), ('Charlie', 16, '10th'),  ('David', 19, '12th'),('Eve', 17, '11th')] cursor.executemany('''INSERT INTO students_table (name, age, grade) VALUES (?, ?, ?)''', students) conn.commit() cursor.execute('SELECT * FROM students_table WHERE age &gt; 18') print("年龄大于18岁的学生:") print(cursor.fetchall()) cursor.execute('UPDATE students_table SET age = age + 1 WHERE name = "Alice"') cursor.execute('DELETE FROM students_table WHERE name = "Bob"') conn.commit() conn.close()</pre>
解析:

评分标准:

(1)students.db 或等效答案;(2分)

(2)CREATE TABLE 或等效答案; (2分)

(3)WHERE age > 18 或等效答案;(3分)

(4)fetchall() 或等效答案。(3分)

3. 简答题

BMI计算器

BMI,身体质量指数,在一定程度反映了人体密度。BMI的计算方法是:体重(kg)除以身高(m)的平方。一般情况下,我国成年人身体质量指数在18.5至23.9内属正常范围,低于18.5表示体重偏瘦,高于23.9表示体重偏胖。    

利用类编写程序计算BMI指数,当输入体重和身高后,输出BMI值,并输出体形情况:偏瘦、偏胖、体形标准。程序部分运行情况如下图所示,请补全以下代码。

class Boy():
    def __init__(self,tizhong,shengao):
        self.tizhong=tizhong
        self.shengao=shengao
    def bmi(self):
        return         ①        
a=int(input('输入体重(kg):'))
b=        ②        
c=Boy(a,b)
if c.bmi()<18.5:
    print("BMI:%d偏瘦。" % c.bmi())
elif         ③        :
    print("BMI:%d偏胖。" % c.bmi())
else:
    print("BMI:%d体形标准。" % c.bmi())
查看答案与解析
答案:<pre class="brush:python;toolbar:false">class Boy():     def __init__(self,tizhong,shengao):         self.tizhong=tizhong         self.shengao=shengao     def bmi(self):         return self.tizhong/(self.shengao*self.shengao) a=int(input('输入体重(kg):')) b=float(input('输入身高(m):')) c=Boy(a,b) if c.bmi()&lt;18.5:     print("BMI:%d偏瘦。" % c.bmi()) elif c.bmi()&gt;23.9:     print("BMI:%d偏胖。" % c.bmi()) else:     print("BMI:%d体形标准。" % c.bmi())</pre>
解析:

评分标准:

(1)self.tizhong/(self.shengao*self.shengao) 或等效答案;(4分)

(2)float(input('输入身高(m):')) 或等效答案;(3分)

(3)c.bmi()>23.9 或等效答案。(3分)