Learn Python The Hard Way学习(35) - 分支和函数

前面我们已经学习了if,函数,list等,现在我们来转变思路,看看你能理解下面的代码吗?
from sys import exit


def gold_room():
print "This room is full of fold. 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 a bear here."
print "The bear has a bunch of honey."
print "The fat bear is in front if 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 our 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 == "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 for 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 "You 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 until you starve.")


start()


运行结果
root@he-desktop:~/mystuff# python ex35.py 
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> right
Here you see the great evil Cthulhu.
He, it, whatever stares at you and you go insane.
Do you flee for your life or eat your head?
> ss
Here you see the great evil Cthulhu.
He, it, whatever stares at you and you go insane.
Do you flee for your life or eat your head?
> flee
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front if another door.
How are you going to move the bear?
> taunt bear
The bear has moved from the door. you can go through it now.
> taunt bear
I got no idea what that means.
> open door
This room is full of fold. How much do you take?
> 4
Man, learn to type a number. Good job!
root@he-desktop:~/mystuff# python ex35.py 
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front if another door.
How are you going to move the bear?
> taunt bear
The bear has moved from the door. you can go through it now.
> open door
This room is full of fold. How much do you take?
> 1
Nice, you are not greedy, you win!

加分练习
1. 画一个游戏地图,看看你玩的流程。

2. 修改你的错误,包括拼写错误。

3. 为函数写评论,还记得doc文档吗?
在函数里面第一行注释,包含在"""中。

4. 扩展你的游戏,你能简洁的扩展它吗?

5. 在gold_room中有一个很不好的方法去判断你输入的是不是数字,你能有更好的方法改进吗?参考int()方法。
if nexe.isdigit():
### 回答1: 《学习Python的硬道理》是一本非常受欢迎的Python编程学习教程。它是由Zed Shaw编写的,以实践为基础的学习方法,具有适合初学者的特点。 这本教程通过一系列的练习和示例,帮助读者逐步掌握Python编程的基础知识和技能。它强调通过不断实践和写代码来学习,而不只是被动地阅读理论知识。实践中的困难会迫使学习者主动思考和解决问题,这也是为什么它被称为"学习Python的硬道理"。 该教程提供了一些实用的技巧和建议,帮助初学者更好地理解和掌握Python编程。通过逐步实现一些小项目和练习题,读者可以逐渐提升自己的编程能力。 与其他教程相比,这本教程更倾向于通过实践学习,并鼓励读者多写代码和尝试不同的方法。这样的学习方式对于编程入门者来说非常有益,因为它使他们能够更深入地理解编程的核心思想和概念。 总而言之,《学习Python的硬道理》是一本很好的Python编程学习教程,特别适合初学者。通过它,读者可以获得坚实的编程基础,并能够在实践中不断提升自己的技能。 ### 回答2: 《Learn Python the Hard Way(笨办法学Python)》是一本广受欢迎的Python编程入门书籍,主要通过实践和练习来帮助读者掌握Python编程语言。 这本书的PDF版本非常受欢迎,因为它提供了免费的学习资源,可以随时随地进行学习。通过下载这个PDF文件,读者可以在没有网络连接的情况下进行学习,并且可以根据自己的进度学习。 《Learn Python the Hard Way》这本书的特点是以实践为重,通过编写和运行代码来学习Python编程。它提供了大量的练习题习题答案,帮助读者巩固所学的知识,并培养解决问题的能力。 这本书适合初学者学习Python编程,无论是有编程经验还是没有编程经验的读者都可以从中受益。它从基础知识开始,逐步介绍了Python的各个方面,包括变量、函数、循环、条件语句等等。通过实践和编程练习,读者可以逐渐掌握Python的编程技巧和应用。 总之,学习Python的最佳途径是通过实践来掌握。《Learn Python the Hard Way》的PDF版本提供了免费的学习资源,可供读者随时随地进行学习。通过实践和编程练习,读者可以快速掌握Python编程的基础知识和技巧。这本书尤其适合初学者,无论有无编程经验,都可以通过学习它来迅速入门Python编程。 ### 回答3: "Learn Python the Hard Way"是一本非常受欢迎的Python编程入门书籍,由Zed A. Shaw撰写。该书旨在帮助初学者通过实际的编程练习来学习Python语言。 这本书的一个主要特点是通过一系列的编程练习来进行学习,每个练习都有详细的说明和示范代码。学生需要阅读相关材料,并按照书中的指导进行代码编写和测试。通过这种刻意练习的方法,学生可以逐步适应并掌握Python的语法、逻辑和编程思维。 "Learn Python the Hard Way"强调了编程的实践重要性,鼓励学生不断尝试、理解和解决问题。通过完成一些较为复杂的编程练习,学生可以逐步提高他们的编程能力和解决问题的技巧。 该书所提供的练习和教学方法不仅适合初学者,也适用于那些希望通过实践来巩固他们的Python知识的中级学习者。 总的来说,"Learn Python the Hard Way"是一本适合通过实践来学习Python编程的入门教材。它通过一系列的编程练习帮助学生逐步掌握Python的基础知识和编程技巧,是一本非常实用和受欢迎的Python学习资源。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值