🎮 一、石头剪刀布游戏
🎯 游戏规则回顾
✊
石头
能砸烂剪刀 ✂️
✂️
剪刀
能剪破布 🧻
🧻
布
能包住石头 ✊
💻 让我们看看代码吧!
import random
# 📝 准备游戏选项
choices = ["石头", "剪刀", "布"]
while True:
print("🎮 欢迎来到石头剪刀布游戏!")
select = int(input("是否开始游戏?(0->退出, 1->开始): "))
if select == 0:
print("👋 再见!感谢游戏!")
break
elif select == 1:
while True:
# 🎯 玩家选择
player = input("请选择(石头/剪刀/布): ")
# ✅ 修正后的判断条件
if player not in choices:
print("🙈 输入错误,请重新选择!")
continue
# 🤖 电脑随机选择
computer = random.choice(choices)
# 📢 显示选择结果
print(f"👤 玩家选择:{player}")
print(f"🤖 电脑选择:{computer}")
# 🏆 判断胜负
if player == computer:
print("🤝 平局!再来一局吧!")
elif (player == "石头" and computer == "剪刀") or \
(player == "剪刀" and computer == "布") or \
(player == "布" and computer == "石头"):
print("🎉 玩家胜利!太棒了!")
else:
print("😅 电脑胜利!继续努力!")
# 问是否继续
again = input("继续游戏吗?(y/n): ")
if again.lower() != 'y':
break
else:
print("🙉 请输入0或1哦!")
💡 点击复制代码
📖 二、新知识点讲解
🔨 神奇的split()函数
🌰 例子:一行输入多个数字
# 用户输入:2 3 5
a, b, h = input().split() # 把字符串按空格分割
print(f"a={a}, b={b}, h={h}") # 输出:a=2, b=3, h=5
🎯 小数保留的魔法
🔢 保留小数的方法
# 方法1:使用 %.1f 格式化
result = 12.566666
print("%.1f" % result) # 输出:12.5
# 方法2:使用 f-string(更简单!)
print(f"{result:.1f}") # 输出:12.5
# 方法3:使用 round() 函数
print(round(result, 1)) # 输出:12.6
📐 三、练习1:求梯形面积
🎯 题目描述
📥 样例输入
2 3 5
📤 样例输出
12.5
# 📝 梯形面积计算程序
# 一行输入三个数,用空格分开
a, b, h = input().split()
# 🔄 把字符串转换成整数
a = int(a)
b = int(b)
h = int(h)
# 🧮 计算面积:(上底 + 下底) × 高 ÷ 2
area = (a + b) * h / 2
# 📋 输出结果,保留1位小数
print("%.1f" % area)
💡 点击复制代码
🏫 四、练习2:计算教室数量
🎯 题目描述
🤔 思路分析
如果1000人,每个教室坐20人:
- 1000 ÷ 20 = 50,刚好整除 → 需要50个教室
- 1001 ÷ 20 = 50余1,有余数 → 需要51个教室(多一个人也要多一个教室!)
📥 样例输入
1000 20
📤 样例输出
50
# 🏫 教室数量计算程序
# 输入总人数和每个教室容量
total_students, room_capacity = input().split()
total_students = int(total_students)
room_capacity = int(room_capacity)
# 🧮 计算需要几个教室
if total_students % room_capacity == 0:
# 整除情况:刚好坐满
rooms_needed = total_students // room_capacity
else:
# 有余数:需要多一个教室
rooms_needed = total_students // room_capacity + 1
print(rooms_needed)
💡 点击复制代码
📅 五、练习3:闰年判断
🎯 题目描述
📏 闰年规则
满足以下任意一个条件就是闰年:
- 能被4整除 但不能 被100整除 (如:2020年)
- 能被400整除 (如:2000年)
📥 样例输入
1996
📤 样例输出
yes
# 📅 闰年判断程序
year = int(input())
# 🧮 闰年判断逻辑
# 方法1:分两种情况判断
if year % 400 == 0:
print("yes") # 能被400整除 → 闰年
elif year % 100 == 0:
print("no") # 能被100整除但不能被400整除 → 平年
elif year % 4 == 0:
print("yes") # 能被4整除但不能被100整除 → 闰年
else:
print("no") # 其他情况 → 平年
# 方法2:一行搞定(更简洁!)
# if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
# print("yes")
# else:
# print("no")
💡 点击复制代码
💪 六、课后练习
📝 单选题 (每题2分,共10分)
1. input().split() 的作用是什么?🤔
2. "%.1f" % 12.567 的输出结果是?💫
3. 在石头剪刀布游戏中,random.choice([1,2,3]) 会返回什么?🎲
4. 判断2024年是否为闰年,下面哪个条件正确?📅
5. 计算教室问题中,1001个学生,每个教室20人,需要几个教室?🏫
🔗 连线题 (每题2分,共10分)
🎊 本课总结
今天我们学会了:
🚀 下节课预告
我们将学习循环结构,让程序能够重复做事情,就像小机器人一样勤劳!🤖
预习任务:思考一下,生活中有哪些重复的事情可以让程序帮我们做?🤔✨