Exercise 35: 分支和函数
原文链接:http://learnpythonthehardway.org/book/ex35.html
你现在已经学习过了 if 语句,函数和列表。现在是时候考验下你了,输入下面这个实例,看看你是否能够弄明白它做了什么。
from sys import exitdef 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'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 = Falsewhile 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. You can go through it now."bear_moved = Trueelif 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()
输出结果如下:
E:\>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 of 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 gold. How much do you take?
> 1000
You greedy bastard! Good job!
研究训练:
1、为这个游戏画一个路线图,看看怎样才能通过这个游戏。
学生遇见的常见问题:
求救!这个程序到底是怎么工作的啊?
答:任何时候在你理解一段代码遇到困难时,你只要简单的为每一行代码写上注释解释它们都做了什么。当你在做这个操作的时候,你得记住以前正确的注释在新的环境和信息下就不一定正确了。接着你就可以试着画一个工作示意图或者写一两段来描述它是如何工作的,这样你就能弄明白了。
为什么使用 while True: ?
答:用于创建一个无限循环操作。
exit(0) 做了什么操作?
答:在很多操作系统中exit(0) 可以用来终止正在运行的项目,并且其中传递的数值可以指明程序是否发生错误。如果你用 exit(1) 就表明出现了一个错误,而 exit(0)就表示正常退出。至于原因你得回到之前学过的布尔逻辑表达式(0 == False),你可以用不同的值来表明不一样的错误结果。你可以用exit(100)来代表一个不同于exit(1)或者exit(0)的错误结果。
为什么 raw_input() 有时候写作 raw_input('> ‘)?
答:raw_input中的参数是一个字符串,用来在获取用户输入之前作为一个提示符而打印出来的。
Exercise 35: 分支和函数相关推荐
- 笔记25 笨办法习题35分支和函数路线图
笔记25 笨办法习题35分支和函数路线图 这似乎是前面ex31游戏的一个延续,这么长的脚本录下来了,用ps执行,很快就纠正若干字码错误,顺利执行完毕.我先试着把这个代码的思路做个整理,理解各个指令的基 ...
- 笨办法学python练习35分支与函数
1. 画一个这个游戏的流程图,并指出它是如何运转的. 3. 为你不理解的函数写上注释. 4. 为游戏增加一些功能,同时使代码更加简化. 这是原代码未经修改 from sys import exit d ...
- 《笨方法学 Python 3》35.分支和函数
基础练习: from sys import exitdef gold_room():print("This room is full of gold. How much do you tak ...
- python 笔记 分支和函数《笨办法学Python》习题35 ——1.17
习题 35: 分支和函数 知识点总结: • 本次做的是利用循环.判定做的一个小游戏 •fromsys import exit #向sys模块借一个exit函数用来退出程序 •exit(0), ...
- 0503、while循环、循环的控制、case分支、函数、字符串的处理
目录 一.while循环,可以根据条件决定循环次数,也可以实现无限循环 二.循环的控制 三.case分支 示例1: 示例2:nginx **nginx 是搭建网站的服务** 安装nginx (1)源码 ...
- case分支 shell函数 字符串的处理
目录 case分支 编写nginx脚本 shell函数 什么是函数? 使用函数的好处? 服务脚本中的函数应用 如何定义一个函数 脚本中断 编写脚本,从1-20中找到6的倍数 编写脚本,for嵌套 字符 ...
- HEVC代码学习35:xEncodeCU函数
xEncodeCU是由encodeCtu调用,其作用是从CTU开始迭代对每个CU进行编码.注意,xEncodeCU是在最优分块已经划分完成后进行编码时使用的,在xCompressCU中没有使用. xE ...
- python教程第四版pdf下载-笨办法学python第四版
笨办法学python第四版是由Zed Shaw所编写的一本书.如果你还是Python新手,那么这是一本非常不错的入门书籍.书本里以习题方式,引导读者慢慢学会了编程. 目录: 习题 0: 准备工作 习题 ...
- python教程第四版pdf下载-笨办法学python第四版 电子书(pdf格式)
笨办法学python第四版是由Zed Shaw所编写的一本书.如果你还是Python新手,那么这是一本非常不错的入门书籍.书本里以习题方式,引导读者慢慢学会了编程. 目录: 习题 0: 准备工作 习题 ...
最新文章
- java C语言内存占用_使用Cgroup限制Java使用的内存量-思路
- 一个小网管的淘金梦----深圳往事(4)
- Android 曝光采集:以商品 view 曝光量的统计为例
- 1249. 移除无效的括号
- treeset java_Java TreeSet add()方法与示例
- 【Hadoop】HIVE 数据表 使用
- “三峡水怪”的真面目竟是这个!水怪:我不要面子的吗?
- 不可小视的贝叶斯(三)
- ubuntu 安装 oracle-xe-universal
- 【转】为什么要用GIT而不是SVN
- 同一账号,后一用户登录,前一个用户则被踢掉
- STM32F427/STM32F437高性能MCU微控制器介绍
- 一把互斥锁保护多个资源
- 开屏展示图的优化与理解
- 腾讯云国外服务器2核4G服务器新用户全攻略
- HAUTOJ 1100~1200题题解
- 分析盘点44,630,000条攻击数据后,创宇蜜罐发现——
- 试题 算法训练 24点 蓝桥杯 Java
- 【技术分享】五:搜索排序-特征分析
- m0n0防火墙的安装(11)