习题 35: 分支和函数

你已经学会了 if 语句、函数、还有列表。现在你要练习扭转一下思维了。把下面的代码写下来,看你是否能弄懂它实现的是什么功能。

 from sys import exit

 def gold_room():
print "This room is full of gold. How much do you take?" next = raw_input("> ")
if "" in next or "" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.") 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 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 = 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 frome 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 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()

你应该看到的结果

下面是我玩游戏的过程:

加分习题

  1. 把这个游戏的地图画出来,把自己的路线也画出来。
  2. 改正你所有的错误,包括拼写错误。
  3. 为你不懂的函数写注解。记得文档注解该怎么写吗?
  4. 为游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能呢?
  5. 这个 gold_room 游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的 bug? 你可以用比检查 0、1 更好的方式判断输入是否是数字吗?int() 这个函数可以给你一些头绪。

习题练习

5.

Python函数-int()

笨办法学Python(三十五)的更多相关文章

  1. 笨办法学Python(十五)

    习题 15: 读取文件 你已经学过了 raw_input 和 argv,这些是你开始学习读取文件的必备基础.你可能需要多多实验才能明白它的工作原理,所以你要细心做练习,并且仔细检查结果.处理文件需要非 ...

  2. 笨办法学Python(十八)

    习题 18: 命名.变量.代码.函数 标题包含的内容够多的吧?接下来我要教你“函数(function)”了!咚咚锵!说到函数,不一样的人会对它有不一样的理解和使用方法,不过我只会教你现在能用到的最简单 ...

  3. 笨办法学Python(十)

    习题 10: 那是什么? 在习题 9 中我你接触了一些新东西.我让你看到两种让字符串扩展到多行的方法.第一种方法是在月份之间用 \n (back-slash n )隔开.这两个字符的作用是在该位置上放 ...

  4. 笨办法学Python(十九)

    习题 19: 函数和变量 函数这个概念也许承载了太多的信息量,不过别担心.只要坚持做这些练习,对照上个练习中的检查点检查一遍这次的联系,你最终会明白这些内容的. 有一个你可能没有注意到的细节,我们现在 ...

  5. 笨办法学Python(十四)

    习题 14:提示和传递 让我们使用 argv 和 raw_input 一起来向用户提一些特别的问题.下一节习题你会学习如何读写文件,这节练习是下节的基础.在这道习题里我们将用略微不同的方法使用 raw ...

  6. 笨办法学Python(十二)

    习题 12:提示别人 当你键入 raw_input() 的时候,你需要键入 ( 和 ) 也就是“括号(parenthesis)”.这和你格式化输出两个以上变量时的情况有点类似,比如说 "%s ...

  7. 笨办法学Python(十六)

    习题 16: 读写文件 如果你做了上一个练习的加分习题,你应该已经了解了各种文件相关的命令(方法/函数).你应该记住的命令如下: close – 关闭文件.跟你编辑器的 文件->保存.. 一个意 ...

  8. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  9. 笨办法学Python - 习题1: A Good First Program

    在windows上安装完Python环境后,开始按照<笨办法学Python>书上介绍的章节进行练习. 习题 1: 第一个程序 第一天主要是介绍了Python中输出函数print的使用方法, ...

  10. 《笨办法学 Python(第四版)》高清PDF|百度网盘免费下载|Python编程

    <笨办法学 Python(第四版)>高清PDF|百度网盘免费下载|Python编程 提取码:jcl8 笨办法学 Python是Zed Shaw 编写的一本Python入门书籍.适合对计算机 ...

随机推荐

  1. js 点击页面出现烟花 心形

    css :1 body{width:1000px; height:500px;} input{ margin:100px; width:250px; height: 50px } 3 .Firewor ...

  2. javasript

    ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECMA,希望这门语言能够成为国 ...

  3. 搭建 Django 平台

    1.使用Pycharm.创建一个Django项目 2.图示 3.在终端输入: 4.结果

  4. NET Core 不错教程***************

    Twinhead 当前标签: .Net Core   RabbitMQ教程 Twinhead 2019-01-26 20:02 阅读:5 评论:0     缓存击穿.缓存穿透和缓存雪崩 Twinhea ...

  5. MyCnblog Style

    以下内容添加到页脚HTML代码处 <style> #leftmenu ul { display: none; } .cnblogs-markdown pre code, .cnblogs- ...

  6. Maven导入jar包

    可在该网址查找:http://search.maven.org/#search%7Cga%7C1%7Cjunit

  7. Robot Framework搭建

    需要安装的内容如下: 1. Python2.7.13(听说python3对RF支持的不是很好,所以我下的Python2) 2. wxPython 2.8.12.1(只能这个版本) 3. robotfr ...

  8. CentOS 7 Linux 卸载/安装 Mariadb MySQL mysql 5.7

    [root@localhost mysql]# ls mysql-community-client--.el7.x86_64.rpm mysql-community-embedded-compat-- ...

  9. mysql初期使用全本

    mysql mysql前戏 数据库服务器-:运行数据库管理软件 =>pc 数据库管理软件:管理-数据库 => mysql 数据库:用来组织文件/表 => 文件夹 表:用来存放多行内容 ...

  10. Python 元组 (tuple)

    作者博文地址:https://www.cnblogs.com/liu-shuai/ Python的元组与列表类似,同样可通过索引访问,支持异构,任意嵌套.不同之处在于元组的元素不能修改.元组使用小括号 ...