2021-09-05

一道上课讲的 蓝桥杯竞赛题

题目:小明坚持每天跑步,正常情况下每天跑一公里,如果这一天是周一或者月初(每月的一号),那么小明就会跑两公里(如果这一天既是周一,又是月初,小明也是跑两公里),小明从2000年1月1日(周六)一直坚持到了2020年10月1日(周四),请你计算一下小明共跑了多少公里?
思路:1.找出星期一或者每月第一天
2.是的话 累加两天 不然每过一天只累加一天
最关键的还是函数包啦 太方便了

from datetime import *
start = date(2000,1,1)
end= date(2020,10,2)
tmp=timedelta(days =1)  #天数每次递加一天
ans =0
while start !=end:
    if start.weekday()==0 or start.day ==1: #星期一对应的是0
        ans+=2
    else:
        ans+=1
    start =start +tmp 
        
print(ans)

提示在这里插入图片描述

题目二

地理老师在黑板上挂了一张世界地图,并给五大洲的每一个洲都标上一个代号,让学生认出五个洲,五个学生分别回答如下
甲:3号是欧洲,2号是美洲;
乙:4号是亚洲,2号是大洋洲;
丙:1号是亚洲,5号是非洲;
丁:4号是非洲,3号是大洋洲;
戊:2号是欧洲,5号是美洲.
老师说他们每人都只说对了一半
请找出编号的对应元素

其实不要那个rjust一样的 我瞎搞的
代码:

five_country=['亚洲','欧洲','非洲','美洲','大洋洲']
for x1 in five_country:
        for x2 in five_country:
                for x3 in five_country:
                        for x4 in five_country:
                                for x5 in five_country:  
                                    if (x2!=x1) and ((x3!=x2) and(x3!=x1)) and ((x4!=x3)and(x4!=x2)and(x4!=x1)) and (x5!=x4) :
                                         if (x3=='欧洲'or x2=='美洲')==1 and (x4=='亚洲' or x2=='大洋洲')==1 and (x1=='亚洲' or x5=='非洲')==1 and (x4=='非洲' or x3=='大洋洲')==1 and (x2=='欧洲' or x5=='美洲')==1 :
                                             
                                            
                                             print('1号是:',x1.rjust(3))            #rjust 一种对齐方式 ljust左对齐 center居中对齐 rjust右对齐 更美观吧
                                             print('2号是:',x2.rjust(3))
                                             print('3号是:',x3.rjust(3))
                                             print('4号是:',x4.rjust(3))
                                             print('5号是:',x5.rjust(3))
                                         
                                             break

题目三

在这里插入图片描述

我其实觉得 这样写 也行啦
不过还是看老师的吧 应该是要学习字典的

print('卡号                 密码')
for i in range(1,101):
    num='61020009%.3d'%(i)   #"d"的话 是三位 f的话是小数点3位 ‘#’
    
    print(num ,'         redhat')

在这里插入图片描述


第四题

万花筒

在这里插入图片描述

import turtle
import random

t=turtle.Pen()
t.speed(0)

width = turtle.window_width()
height=turtle.window_height()

turtle.bgcolor('white')
colors=['red','orange','blue','green','purple','pink','yellow']

#画螺旋线  x,y落笔的位置 ;边数
def draw_spiral(x,y,sides):
    t.penup()
    t.setpos(x,y)
    t.pendown()
    for m in range(sides):
        t.forward(2*m)
        t.left(61)

        
def draw_wanhuatong(x,y):
    t.color(random.choice(colors))
    sides = random.randint(10,50)


    draw_spiral(x,y,sides)
    draw_spiral(-x,y,sides)
    draw_spiral(-x,-y,sides)
    draw_spiral(x,-y,sides)
turtle.onscreenclick(draw_wanhuatong)

题目五

一个小球游戏 (还没做完) 小球碰撞后会弹开
小球的图片:
需要自己保存名字为 beach_ball.png
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

import sys, pygame
from random import choice


class Ball(pygame.sprite.Sprite):
    def __init__(self,image_file,location, speed):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
        self.speed =speed
        
    def move(self):
        self .rect=self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0]= -self.speed[0]
                
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1]= -self.speed[1]
            
def animate(group):
    screen.fill([255,255,255])
    for ball in group:
        group.remove(ball)
        if pygame.sprite.spritecollide(ball,group,False):
            ball.speed[0] = -ball.speed[0]
            ball.speed[1] = -ball.speed[1]
        group.add(ball)
        ball.move()
        screen.blit(ball.image,ball.rect)

    pygame.display.flip()
    pygame.time.delay(20)
        
        
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])

img_file="beach_ball.png"
group=pygame.sprite.Group()

for row in range (0,2):
    for column in range (0,2):
        location = [column *180 + 10, row * 180 + 10]
        speed=[choice([-2,2]),choice([-2,2])]
        ball = Ball(img_file,location,speed)
        group.add(ball)
        
    # Adds balls to a list


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    animate(group)
pygame.quit()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用python中的pymsql完成如下:表结构与数据创建 1. 建立 `users` 表和 `orders` 表。 `users` 表有用户ID、用户名、年龄字段,(id,name,age) `orders` 表有订单ID、订单日期、订单金额,用户id字段。(id,order_date,amount,user_id) 2 两表的id作为主键,`orders` 表用户id为users的外键 3 插入数据 `users` (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28); `orders` (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4); 查询语句 1. 查询订单总金额 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 3. 查询订单总数最多的用户的姓名和订单总数。 4. 查询所有不重复的年龄。 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 8. 查询订单总金额最大的用户的姓名和订单总金额。 9. 查询订单总金额最小的用户的姓名和订单总金额。 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。
最新发布
06-03
这里是使用 Python 的 PyMySQL 完成上述任务的代码: ```python import pymysql # 连接数据库 conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', database='test') # 创建游标 cur = conn.cursor() # 创建 users 表 cur.execute(''' CREATE TABLE users ( id INT PRIMARY KEY, name VARCHAR(20), age INT ) ''') # 创建 orders 表 cur.execute(''' CREATE TABLE orders ( id INT PRIMARY KEY, order_date DATE, amount INT, user_id INT, FOREIGN KEY (user_id) REFERENCES users(id) ) ''') # 插入数据 users_data = [ (1, '张三', 18), (2, '李四', 20), (3, '王五', 22), (4, '赵六', 25), (5, '钱七', 28) ] orders_data = [ (1, '2021-09-01', 500, 1), (2, '2021-09-02', 1000, 2), (3, '2021-09-03', 600, 3), (4, '2021-09-04', 800, 4), (5, '2021-09-05', 1500, 5), (6, '2021-09-06', 1200, 3), (7, '2021-09-07', 2000, 1), (8, '2021-09-08', 300, 2), (9, '2021-09-09', 700, 5), (10, '2021-09-10', 900, 4) ] for user in users_data: cur.execute('INSERT INTO users VALUES (%s, %s, %s)', user) for order in orders_data: cur.execute('INSERT INTO orders VALUES (%s, %s, %s, %s)', order) # 提交事务 conn.commit() # 查询语句 # 1. 查询订单总金额 cur.execute('SELECT SUM(amount) FROM orders') print(cur.fetchone()[0]) # 2. 查询所有用户的平均年龄,并将结果四舍五入保留两位小数。 cur.execute('SELECT ROUND(AVG(age), 2) FROM users') print(cur.fetchone()[0]) # 3. 查询订单总数最多的用户的姓名和订单总数。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_orders DESC LIMIT 1 ''') print(cur.fetchone()) # 4. 查询所有不重复的年龄。 cur.execute('SELECT DISTINCT age FROM users') print([row[0] for row in cur.fetchall()]) # 5. 查询订单日期在2021年9月1日至9月4日之间的订单总金额。 cur.execute('SELECT SUM(amount) FROM orders WHERE order_date BETWEEN "2021-09-01" AND "2021-09-04"') print(cur.fetchone()[0]) # 6. 查询年龄不大于25岁的用户的订单数量,并按照降序排序。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders FROM users JOIN orders ON users.id = orders.user_id WHERE age <= 25 GROUP BY users.id ORDER BY total_orders DESC ''') print(cur.fetchall()) # 7. 查询订单总金额排名前3的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC LIMIT 3 ''') print(cur.fetchall()) # 8. 查询订单总金额最大的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC LIMIT 1 ''') print(cur.fetchone()) # 9. 查询订单总金额最小的用户的姓名和订单总金额。 cur.execute(''' SELECT users.name, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount ASC LIMIT 1 ''') print(cur.fetchone()) # 10. 查询所有名字中含有“李”的用户,按照名字升序排序。 cur.execute('SELECT * FROM users WHERE name LIKE "%李%" ORDER BY name ASC') print(cur.fetchall()) # 11. 查询所有年龄大于20岁的用户,按照年龄降序排序,并只显示前5条记录。 cur.execute('SELECT * FROM users WHERE age > 20 ORDER BY age DESC LIMIT 5') print(cur.fetchall()) # 12. 查询每个用户的订单数量和订单总金额,并按照总金额降序排序。 cur.execute(''' SELECT users.name, COUNT(*) AS total_orders, SUM(amount) AS total_amount FROM users JOIN orders ON users.id = orders.user_id GROUP BY users.id ORDER BY total_amount DESC ''') print(cur.fetchall()) # 关闭游标和连接 cur.close() conn.close() ``` 注意:在运行代码之前,需要先安装 PyMySQL 模块,可以使用以下命令进行安装: ``` pip install pymysql ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值