习题 35: 分支和函数
【扫盲】
开始做练习之前,我的习惯先把代码阅读一遍,挑出模棱两可的点,上网查查,然后根据文档写个demo敲一遍。然后再回头完整看代码。
来,跟着本小姐姐来瞅瞅吧,嘻嘻嘻…
1、if …in…
作用:判断一个字符串是否包含子串
使用成员操作符 in
2、exit()函数
- exit(0):无错误退出
- exit(1):有错误退出
3、自定义dead函数
作用:输出提示,退出打印
def dead(why):
print(why,"Good job!")
exit(0)
4、int()函数
python的内置函数,作用:强制转换为整型
补充:判断是否为数字str.isdigit()
如果 string 只包含数字(这里负数是不可以的)则返回 True 否则返回 False.
next = input(">>>> ")
print(next.isdigit())
if next.isdigit() == True:
print("是数字")
elif next.isdigit() == False:
print("不是")
【分析】
首先gold_room。这个函数是有问题的,逻辑以及写法上都要调整。这一点作者在加分题的第5问提出来了。下面我们抽出来这段代码看一下
def gold_room():
print("This room is full of gold. How much do you take?")
next = input("> ")
# 根据else分支可以判断,if分支是想判断用户输入是整型数字。
# 但这种写法有bug,只能判断字符串里是否含有0或者1。输入数字2时,就会走else分支了,显然逻辑是不对的
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man,learn to type a number.")
# 由于上面的判断,导致了只有输入含有0或者1且数字小于50才走下面if分支
# else 分支永远执行不到,全被上面的else拦截了
if how_much < 50:
print("Nice,you're not greedy,you win!")
exit(0)
else:
dead("You greedy bastard!")
根据作者提示(int()函数,上网查了廖雪峰博客),以及网上说的isdigint函数。我做了修改,请指正:
def gold_room():
print("这个房间充满了黄金,你准备拿多少?")
next = input("请输入你想拿的数量:")
if next.isdigit() == True:
how_much = int(next)
else:
dead("请输入数字!")
if how_much < 50:
dead("恭喜你,你不贪婪,你赢了!")
else:
dead("很遗憾,你太贪婪了!")
最后贴上完整代码:
# coding:utf-8
# 把sys模块里的exit函数引用过来,覆盖默认了exit()
from sys import exit
# 黄金屋
def gold_room():
print("This room is full of gold.How much do you take?")
# 提示用户输入
next = input(">>>> ")
if str.isdigit(next) == True:
how_much = int(next)
else:
dead("Man,learn to type a number.")
if how_much < 50:
dead("Nice,you're not greedy,you win!")
else:
dead("You greedy bastard!")
# 熊屋子:这是个死循环,必须找到一种出路
def bear_room():
print("There is a bear here.")
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 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.")
# cthulhu屋子:有个自我调用
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 for 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 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_room()
else:
dead("You stumble around the room until you starve.")
def dead(why):
print(why,"Good job!")
exit(0)
# 函数入口
start()
"""
加分习题
1.把这个游戏的地图画出来,把自己的路线也画出来
2.改正你所有的错误,包括拼写的错误
3.为你不懂的函数写注解。记得文档注解该怎么写吗?
三引号,或者#
4.为这个游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能呢?
5.这个gold_room游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的bug?
你可以用比检查0,1更好的方式判断输入是否是数字吗?
int()这个函数可以给你一些头绪
"""