题目
我们已经学会了 if
语句、函数还有列表。现在我们需要搞清楚本题代码实现的是什么功能。
加分练习
- 把这个游戏的地图画出来,把自己的路线也画出来。
- 改正你所有的错误,包括拼写错误。
- 为不懂的地方写注解。
- 为游戏添加更多元素。通过怎样得方式可以简化并且扩展游戏的功能呢?
- 这个
gold_room
游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的 bug ?你可以用比检查 0、1 更好的方式判断输入是否是数字吗?int()
这个函数可以给你一些头绪。
我的答案
35.0 基础练习 + 35.2除错 + 35.3 注解
# sys.exit 用于结束程序
from sys import exit
# 进入黄金房间后的逻辑
def gold_room():
print("This room is full of gold. How much do you take?")
# 如果输入不包含 0 或 1 则死
next = input("> ")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")
# 如果输入的数字大于等于 50 则死
if how_much < 50:
print("Nice, you're not greedy, you win!")
exit(0)
else:
dead("You greedy bastard!")
# 实现熊房间的逻辑
def bear_room():
print("There is a bear hear.")
print("The bear has a bunch of honey.")
print("The fat bear is in front of another door.")
print("How are you going to move the bear?")
bear_moved = False
# 如果熊离开后直接开门就用不到 while 循环了.
while True:
next = input("> ")
if next == "take honey":
dead("The bear looks at you then slaps your face off.")
elif next == "taunt bear" and not bear_moved:
print("The bear has moved from the door. You can go through it now.")
bear_moved = True
elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed off and chews your leg off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print("I got no idea what that means.")
# 恶魔房逻辑
def cthulhu_room():
print("Here you see the great evil Cthulhu.")
print("He, it, whatever stares at you and you go insane.")
print("Do you flee your life or eat your head?")
next = input("> ")
# 二选一,否则恶魔放循环
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
# 惨死函数
def dead(why):
print(why, "Good job")
exit(0)
# 启动函数
def start():
print("You are in a dark room.")
print("There is a door to your right and left.")
print("Which one do you take?")
next = input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_roome()
else:
dead("You stumble around the room until you starve.")
# 开始游戏
start()
目测这款游戏想打通关可不容易,让我们来些攻略吧
35.1 游戏地图(攻略)
我发现自己似乎不太擅长画流程图啊
35.4 添加元素
比较方便的方法还是想办法写函数吧,不过因为文本量比大所以感觉仍然不是很方便。
35.5 gold_room 的 bug
可以直接改这样
next = input("> ")
how_much = int(next)
不过如果 next 不是数字文本的话就会出错,不知道在当前知识进度下有没有其他好办法。