Python上机作业 --- 4

一、

动画:皮球从高处落地弹起.
提示:
假设速度矢量向下为正,向上为负(注意坐标系方向),
且以皮球距地面高度(而非纵坐标)为位置变量,
则主要计算公式为
球的新高度 = 球的旧高度 - 球速 * 时间间隔
球的新速度 = 球的旧速度 + 9.8 * 时间间隔 y h
落地反弹:if 新高度 < 球半径:
应设置新高度=球半径
然后速度方向取反

import tkinter as tk 
import time
window = tk.Tk()
window.geometry("200x600")
window.resizable(height=False, width=False)
window.title('Animation Created by Cyberist')
canvas = tk.Canvas(window, width=200, height=600)
radius = 20

ball = canvas.create_oval(100,80, 140, 40, fill='brown')

canvas.create_rectangle(0, 550,600,560,fill='red')

old_height = 490
old_velocity = 0
t = 0.05

canvas.pack()
while True:
    
    new_height = old_height - old_velocity * t # 新的高度
    new_velocity = old_velocity + 9.8 * t  # 新的速度
    
    if new_height < 20: # 高度小于20的时候重置
        new_height = 20
        new_velocity = -1* new_velocity
        
    elif new_height > 490:
        new_height = 490
        new_velocity = 0 

    else:
        diff_height = old_height - new_height
        canvas.move(ball, 0, diff_height)
         
    old_height = new_height
    old_velocity = new_velocity
    
    canvas.update()
    time.sleep(0.02)


window.mainloop()

二、

从ftp下载人口普查数据文件data.txt,文件每一行是用逗号分隔的15个数据。如第1行是:
25, Private, 226802, 11th, 7, Never-married, Machine-op-inspct, Own-child, Black, Male, 0, 0, 40, United-States, <=50K.

15个数据中,第4,9,10,15个数据的含义如下(x忽略):
x,x,x,教育程度,x,x,x,x,种族,性别,x,x,x,x,收入
这四个数据可能取的值分别是:
教育程度值: Preschool, 1st-4th, 5th-6th, 7th-8th, 9th, 10th, 11th,12th, HS-grad, Assoc-acdm, Assoc-voc, Prof-school, Some-college, Bachelors, Masters, Doctorate.
种族值: White, Black, Asian-Pac-Islander, Amer-Indian-Eskimo, Other.
性别值: Female, Male.
收入值:<=50K, >50K

按如下分类,分别统计各类人群的收入情况,即某类人群中<=50K和>50K的各占多少?
教育:有学位的人(Bachelors/Masters/Doctorate) vs 其他人
种族:白人(White)vs 其他人
性别:男人(Male)vs 女人(Female)

用图形给出直观的结果(例如用分为两色的柱状图表示各占的比例)。如果时间不够,以文字形式输出。

import tkinter as tk 
file = open('./data.txt', 'r')
# 有学位之类的
educated_gt_50 =0
educated_lt_50 = 0 

other_educated_gt_50 = 0 
other_educated_lt_50 = 0 

# 白色人种之类的
white_gt_50 = 0 
white_lt_50 = 0 

other_skin_gt_50 = 0
other_skin_lt_50 = 0 

# 男性和女性
male_gt_50 = 0 
male_lt_50 = 0 

female_gt_50 = 0 
female_lt_50 = 0

def draw(canvas,num,text,gt_50_1,lt_50_1,gt_50_2,lt_50_2):
    
    x0 = 30 + (num-1) * 100  # 计算初始位置
    y0 = 360 - gt_50_1/10  # 左上端的显示位置
    x1 = x0 + 30  # 右下角的位置
    canvas.create_rectangle(x0, y0, x1, 360, fill='red') # 第一类高收入的
    canvas.create_text(x0-10,y0, text='{:.2f}%'.format(gt_50_1/(gt_50_1+lt_50_1)*100), anchor=tk.NW)
    y1 = lt_50_1 /10  # 

    # 下方的文字提示
    canvas.create_text(x0,360, text=text[0],anchor=tk.NW)
    canvas.create_text(x0+45,360,text= text[1],anchor=tk.NW)

    canvas.create_rectangle(x0,y0-y1, x1,y0, fill='orange') # 第一类低收入
    
    percentage_1 =lt_50_1/(gt_50_1+lt_50_1) 
    # 上面部分的百分比
    canvas.create_text(x0,y0-y1, text="{:.2f}%".format(percentage_1*100), anchor=tk.NW)

    x2 = x1+30 
    y2 = 360 - gt_50_2/10 
    canvas.create_rectangle(x1, y2, x2, 360, fill='red') # 第二类高收入的
    y3 = lt_50_2 /10
    canvas.create_text(x1,y2, text='{:.2f}%'.format(gt_50_2/(gt_50_2+lt_50_2)*100), anchor=tk.NW)
    canvas.create_rectangle(x1,y2-y3, x2,y2, fill='orange') # 第二类低收入的
    percentage_2 = lt_50_2/(gt_50_2+lt_50_2)
    canvas.create_text(x1,y2-y3, text="{:.2f}%".format(percentage_2*100), anchor=tk.NW)


for data in file.readlines():
    list_ = data.rstrip('.').split(',')

    if "Mas" in list_[3] or   "Doc" in list_[3] or "Bac" in list_[3]:
        if '>' in list_[14] :
        
#             print(list_[3],list_[14])
            educated_gt_50 += 1 
        else:
            educated_lt_50 += 1
    else:
        if '>' in list_[14]:
            other_educated_gt_50 += 1 
        else:
            other_educated_lt_50 += 1 
            
    if 'White' in list_[8]:
        if '>' in list_[14]:
            white_gt_50 += 1
        else:
            white_lt_50 += 1 
    else:
        if '>' in list_[14]:
            other_skin_gt_50 += 1 
        else:
            other_skin_lt_50 += 1 
    
    if 'Male' in list_[9]:
        if ">" in list_[14]:
            male_gt_50 += 1
        else:
            male_lt_50 += 1
    else:
        if ">" in list_[14]:
            female_gt_50 += 1
        else:
            female_lt_50 += 1
            
#     print(list_[3],list_[8],list_[9],list_[14])


t1=["有\n学\n位","无\n学\n位",]
t2=["白\n人","有\n色\n人",]
t3=["男\n性","女\n性",]

window = tk.Tk()
window.title("Income analysis") 
window.geometry("300x450")
canvas = tk.Canvas(window, width=300,height=450)
canvas.create_text(150,30, text='The income analysis',font='Monano 18', anchor=tk.CENTER)
draw(canvas,1,t1,educated_gt_50,educated_lt_50,other_educated_gt_50,other_educated_lt_50)
draw(canvas,2,t2, white_gt_50,white_lt_50,other_skin_gt_50,other_skin_lt_50)
draw(canvas,3,t3, male_gt_50,male_lt_50, female_gt_50,female_lt_50)

canvas.pack()
window.mainloop()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值