笨方法学Python笔记(7)

下班了,可是本宝宝还要学习,沉迷学习不能自拔。

习题35:分支和函数

这一个习题的代码好长啊,敲得我手酸、、、

from sys import exit

def gold_room():
	print "This room is full of gold. How much do you take?"
	
	next = raw_input("> ")
	if "0" in next or "1" in next:
		how_much = int(next)
	else:
		dead("Man, learn to type a number.")
	if how_much < 50:
		print "Nice, you are not greedy, you win!"
		exit(0)
	else:
		dead("You greedy bastard!")

		
def bear_room():
	print "There is s 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 True:
		next = raw_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. Your 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 starts at you and you go insane."
	print "Do you flee your life or eat your head?"
	
	next = raw_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 "Your are in a dark room."
	print "There is a door to your right and left."
	print "Which one do you take?"

	next = raw_input("> ")
	
	
	if next =="left":
		bear_room()
	elif next == "right":
		cthulhu_room()
	else:
		dead("You stumble around the room untill you starve.")
		
start()

笨方法学Python笔记(7)_第1张图片 笨方法学Python笔记(7)_第2张图片

玩了一下,作者的脑洞也真是大,延续了恐怖故事的风格。发现外国人对熊比较钟爱,要是国内的人写,估计应该选的是老虎、狼了。

1. 把这个游戏的地图画出来,把自己的路线也画出来。

答:(⊙o⊙)哦,竟然要画图、、、本宝宝可是灵魂画手,终于有用武之地了。来,图。一不小心图有点大,凑合看吧。从图中我们可以清晰地看出来,the great evil Cthulhu有多么可怕,简直吓死人。Bear也好怕怕,吓死宝宝了。如果有喜欢本人绘画作品的请随时联系,价格可以商榷,绝对良心。

笨方法学Python笔记(7)_第3张图片

2. 改正你所有的错误,包括拼写错误。

答:犯了一个错,就是if后面的判断条件中的是==不是=

3. 为你不懂的函数写注解。记得文档注解该怎么写吗?

答:当然记得啦,用“““和”””把注解引起来就好。这个等我慢慢看一下。

4. 为游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能呢?

答:这个游戏画风太恐怖,本宝宝可不可以选择拒绝、、、内心极不情愿(强颜欢笑中)。But、、、还是乖乖照着做的。我优化了一下逻辑分支和判断方式,把while改成了for、把print部分写成了几个函数进行分装。代码如下:

from sys import exit

def gold_room():
	print "This room is full of gold. How much do you take?"
	next = raw_input("> ")
	if next.isdigit():
		how_much = int(next)
	else:
		dead("Man, learn to type a number.")
	if how_much < 50:
		print "Nice, you are not greedy, you win!"
		exit(0)
	else:
		dead("You greedy bastard!")

def bear_room():
	hit1()
	next = raw_input("> ")
	if next == "1":
		dead("The bear looks at you then slaps your face off!")
	elif next =="2":
		hit2()
		next = raw_input("> ")
		if next == "1":
			gold_room()
		elif next == "2":
			dead("The bear gets pissed off and chews your leg off.")
		else:
			hit3()
	else:
		hit3()
			
def cthulhu_room():
	hit4()
	next = raw_input("> ")
	if next =="1":
		start()
	elif next == "2":
		dead("Well that was tasty!")
	else:
		cthulhu_room()

def dead(why):
	print why,"Good job!"
	exit(0)

def hit0():
	print "Your are in a dark room."
	print "There is a door to your right and left."
	print "Which one do you take?"

def hit1():
	print "There is s 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?"
	print "1.Take the honey."
	print "2.taunt bear."

def hit2():
	print "The bear has moved from the door. Your can go through it now."
	print "1.open the door."
	print "2.taunt bear."

def hit3():
	print "I got no idea what that means."

def hit4():
	print "Here you see the great evil Cthulhu."
	print "He, it ,whatever starts at you and you go insane."
	print "Do you 1.flee your life or 2.eat your head?"

def start():
	hit0()
	next = raw_input("> ")
	if next =="left":
		bear_room()
	elif next == "right":
		cthulhu_room()
	else:
		dead("You stumble around the room untill you starve.")
		
start()



5. 这个 gold_room 游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的 bug? 你可以用比检查 0、1 更好的方式判断输入是否是数字吗?int() 这个函数可以给你一些头绪。

答:代码中使用了

if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("Man, learn to type a number.")

来给how_much进行赋值。这样写的问题就是如果输入的是2、22这些不带0和1的数字,那么就会走入错误的逻辑分支。查了一下,用isdigit函数可以实现判断是否为数字的功能。如下:

if next.isdigit():
        how_much = int(next)

=======================================================================================总结:这个习题目前为止最复杂的一道了,集合了函数、逻辑判断、导入模组等等之前很多知识点。虽然作者喜欢暴力恐怖的故事,还是要感谢作者这么一步一个脚印教我们这些小白啦~~~~

你可能感兴趣的:(python学习)