做出选择
为故事创建分支
做出选择 是 CoddyKit 上的免费 Coding for Kids 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Coding for Kids 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Coding for Kids 课程共包含 4 节课。
选择您的道路!🌳
又见面了!🏴☠️ 最精彩的冒险会让 YOU 选择接下来发生什么!🌳
今天我们要学习使用选择和if让故事产生分支!🤔
print("Time to make choices! 🌳")提供两条道路 🛤️
给玩家两条道路供他们选择!🛤️
然后询问他们选择哪一条!
print("A fork in the road! 🛤️")
print("Left = cave 🕳️")
print("Right = river 🌊")
choice = input("Which way? (left/right) ")
print("You chose:", choice)使用 if 做出反应 🧠
现在,我们使用 if 让游戏对您的选择 REACT!🧠
不同的选择会带来不同的故事!✨
choice = "left"
if choice == "left":
print("You enter a spooky cave! 🕳️")
else:
print("You swim across the river! 🌊")不止两条道路 🔀
使用elif添加更多 MORE 道路!🔀
elif的意思是“否则,如果是这个选项”!🤓
choice = "climb"
if choice == "cave":
print("Into the cave! 🕳️")
elif choice == "climb":
print("You climb a tall tree! 🌳")
else:
print("You wait around. 🤷")匹配真实选择 🎯
让我们把玩家真正的输入与故事连接起来!🎯
door = input("Open the red or blue door? ")
if door == "red":
print("A friendly monkey! 🐵")
else:
print("A pile of gold! 🪙")注意拼写!🔤
计算机很挑剔!对 Python 来说,"Left"和"left"是 DIFFERENT 的!😮
使用.lower()将所有内容转换为小写,避免混淆!🪄
choice = "LEFT"
choice = choice.lower()
if choice == "left":
print("Going left! 👈")选择会改变状态 📊
选择可以改变玩家的金币或生命值!📊
拾取黄金会增加金币!🪙
coins = 0
choice = "grab gold"
if choice == "grab gold":
coins = coins + 10
print("Coins now:", coins, "🪙")危险的选择 ⚠️
有些选择很 RISKY!碰到陷阱会损失生命值!⚠️
health = 100
choice = "touch trap"
if choice == "touch trap":
health = health - 30
print("Health now:", health, "❤️")选择中的选择 🪆
一个选择可能会引出 ANOTHER 个选择!就像套娃一样!🪆
path = "cave"
if path == "cave":
print("Inside the cave you see a chest! 📦")
print("Do you open it? Next lesson decides! 😏")选择函数 🛠️
把选择封装进一个函数,就可以重复使用了!🛠️
def ask(question):
answer = input(question + " ")
return answer.lower()
pick = ask("Go up or down?")
print("You picked:", pick)选择大师!🏆
现在您可以让任何故事产生分支了!🎉
if会对选择做出反应 🧠elif可以添加更多道路 🔀.lower()可以处理拼写差异 🔤- 选择会改变金币和生命值 📊
接下来:寻找宝藏并 WINNING!💎
print("I can make choices! 🌳🥇")快速测验 ✅
让我们测试一下您的选择能力吧! 🧠
回顾时间! 🎒
做得好,选择高手! 🌟
if/elif/else让故事分支发展 🌳.lower()可以避免拼写混淆 🔤- 选择会改变 金币 和 生命值 📊
- 一个选择可能带来更多选择 🪆
接下来:WINNING 宝藏! 💎
print("Making Choices: complete! 🚀")常见问题解答
「做出选择」课时是免费的吗?
是的 — 「做出选择」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Coding for Kids 课程的其余内容,请升级到 CoddyKit PRO。 Coding for Kids 课程共包含 4 节课。
「做出选择」这节课中我会学到什么?
为故事创建分支 你通过在浏览器中直接运行的动手代码来练习 Coding for Kids,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Coding for Kids 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Coding for Kids 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「做出选择」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Coding for Kids 课中编写并运行代码吗?
能。每节 Coding for Kids 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。