目录
1.编写程序,统计该航班50岁以上男(含50))、女乘客的人数以及年龄最大的乘客姓名、年龄和座位。
import csv
def count_passengers(file_path):
num_females_above_50 = 0
num_males_above_50 = 0
max_age = -1
max_age_name = ""
max_age_seat = ""
with open(file_path, newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# 统计50岁以上旅客的数量
if int(row['Age']) >= 50:
if row['Sex'] == 'female':
num_females_above_50 += 1
elif row['Sex'] == 'male':
num_males_above_50 += 1
# 找到年龄最大的旅客
age = int(row['Age'])
if age > max_age:
max_age = age
max_age_name = row['Name']
max_age_seat = row['Seat']
print(f"Number of females above 50: {num_females_above_50}")
print(f"Number of males above 50: {num_males_above_50}")
print(f"Max age: {max_age}, Name: {max_age_name}, Seat: {max_age_seat}")
count_passengers('flight.csv')
2. 设计一个简易的卡路里计算器。
窗体大小为400*300,在窗体上放置标签、输入框、单选框和按钮等控件。输入运动的时间(分钟),选择运动的种类(每小时卡路里消耗为:跑步:500,游泳:630,打篮球:700),单击“换算”按钮,在标签上显示消耗的卡路里(小数点后保留两位)。
使用 Python 的 tkinter 模块来创建 GUI 程序
import tkinter as tk
class CalorieCalculator:
def __init__(self, master):
self.master = master
master.title("Calorie Calculator")
# 添加标签
self.label_time = tk.Label(master, text="Time (mins):")
self.label_time.grid(row=0, column=0)
self.label_type = tk.Label(master, text="Type of Exercise:")
self.label_type.grid(row=1, column=0)
self.label_result = tk.Label(master, text="Result:")
self.label_result.grid(row=2, column=0)
# 添加输入框
self.entry_time = tk.Entry(master)
self.entry_time.grid(row=0, column=1)
# 添加单选框
self.var_type = tk.StringVar(value="running")
self.radio_running = tk.Radiobutton(master, text="Running (500 cals/hr)", variable=self.var_type, value="running")
self.radio_running.grid(row=1, column=1, sticky="w")
self.radio_swimming = tk.Radiobutton(master, text="Swimming (630 cals/hr)", variable=self.var_type, value="swimming")
self.radio_swimming.grid(row=2, column=1, sticky="w")
self.radio_basketball = tk.Radiobutton(master, text="Basketball (700 cals/hr)", variable=self.var_type, value="basketball")
self.radio_basketball.grid(row=3, column=1, sticky="w")
# 添加按钮
self.button_convert = tk.Button(master, text="Convert to Calories", command=self.convert)
self.button_convert.grid(row=4, column=0, columnspan=2)
def convert(self):
time_in_mins = int(self.entry_time.get())
exercise_type = self.var_type.get()
if exercise_type == "running":
calorie_per_hour = 500
elif exercise_type == "swimming":
calorie_per_hour = 630
elif exercise_type == "basketball":
calorie_per_hour = 700
calorie = round((calorie_per_hour / 60) * time_in_mins, 2)
self.label_result.config(text=f"Result: {calorie} cals")
root = tk.Tk()
calculator = CalorieCalculator(root)
root.mainloop()
3.编写程序,创建300*300画布,以画布中心为原点画出坐标轴,按以下公式绘制函数曲线,要求使用Canvas或者Turtle绘图方法实现(使用其他方法绘图不得分)。
y =t*sin(1/t)其中t的取值范围为-1至1,步长为0.01。注意:t不能为0,x,y轴的放大倍数均为300倍。
import turtle
import math
def draw_function():
screen = turtle.Screen()
screen.setup(width=600, height=600)
screen.setworldcoordinates(-300, -300, 300, 300)
t = -1.0
turtle.up()
turtle.goto(t * 300, math.sin(1 / t) * 300)
turtle.down()
while t < 1.0:
x = t * 300
y = math.sin(1 / t) * 300
turtle.goto(x, y)
t += 0.01
turtle.done()
draw_function()