笨办法学python习题31 做出决定
来源:互联网 发布:数据量化方法 编辑:程序博客网 时间:2024/05/20 05:07
这次的习题学习的是if/elif/else的用法,另外在附加习题中添加一些判断条件时,对raw_input的用法更深刻。
首先还是上代码(包含附加题中新增的一段):
print"You enter a dark room with two doors. Do you go through door #1 or door #2 or door #3?"door = raw_input(">")if door == "1":print"There's a giant bear here eating a cheese cake.What do you do?"print"1.Take the cake."print"2.Scream at the bear."bear = raw_input(">")if bear =="1":print"The bear eats your face off. Good job!"elif bear == "2":print"The bear eats your legs off. Good job"else:print"Well,doing %s is probably better.Bear runs away." %bearelif door =="2":print"You stare into the endless abyss at cthulhu's retina."print"1.Blueberries."print"2.Yellow jacket clothespins."print"3.Understanding revolvers yelling meldies."insanity = raw_input(">")if insanity =="1" or insanity =="2":print"Your body survives powered by a mind of jello. Good job!"else:print"The insanity rots your eyes into a pool of muck. Good job!"elif door == "3":print"""There is a angle in front of you,He will help you out.Please pray"""pray_count = raw_input(">")if pray_count == "500":print"The angle help you out,Congratulation!"else:print"The angle can't decide."else:print"You stumble around and fall on a knife and die. Good job"1,首先说明一下if 和 elif else在使用上的区别:
①if语句的每一个条件都会进行判断,在程序运行过程中会对if语句的判断条件进行遍历判断,耗时较多
②if 语句中可以有多个elif的判断条件,但结尾只有一个else语句。
③elif后面接判断条件,else无判断条件。
④举个栗子,说明if与elif的区别:
a= True , b = True
if a:
print"a"
if b:
print"b"
这种写发,输出的是a和b。
a= True , b = True
if a:
print"a"
elif b:
print"b"
这种写法,输出的是aa= False , b = True
if a:
print"a"
elif b:
print"b"
这种写法,输出的是b.elif的意思是“否则的话,如果……”
2,最后说一下raw_input的部分,在附加题中自己新增的一段代码如下:
elif door == "3":print"""There is a angle in front of you,He will help you out.Please pray"""pray_count = raw_input(">")if pray_count == "500":print"The angle help you out,Congratulation!"else:print"The angle can't decide."
黄色mark部分,前面写的时候写成if pray_count == 500:
这个时候不论输入的是不是500,输出永远是:The angle can't decide.后面想到“raw_input() 将所有输入作为字符串看待,返回字符串类型”,因此在将500修改为“500”后异常得以解除。
再小的技能都值得认真对待!!!
阅读全文
0 0
- 笨办法学python习题31 做出决定
- 笨方法学python--习题31---做出决定。
- 习题31 做出决定
- 笨方法学Python 习题 31: 作出决定
- sudsn 笨办法学Python 习题13
- 《笨办法学python》习题38 40
- 笨办法学python之习题26
- 笨办法学python 习题46
- 笨办法学python 习题46-windows
- 笨办法学Python-习题0 准备工作
- 笨办法学Python习题11 提问
- 笨办法学python 习题24
- 笨办法学python习题3 数字和数学计算
- “笨办法学python”学习笔记-习题5
- “笨办法”学Python习题41代码问题
- 笨办法学Python笔记-习题0:准备工作
- 笨办法学Python笔记-习题1:第一个程序
- 笨办法学Python-习题2:注释和井号
- _sys_exit( int x) { x = x; } 报错
- Internet Explorer
- 可变参数列表求最大值与平均值
- Findmaxmin递归算法实现
- 逻辑运算符与位运算符以及三目运算符
- 笨办法学python习题31 做出决定
- Arduino--读u8lib之I2C代码
- BZOJ 1191 [HNOI2006]超级英雄Hero(二分图匹配)
- Spring与HikariCP的结合使用
- Java web项目--Spring数据源切换
- mariadb-server
- 二叉树
- 猜数字游戏
- 【PHP】XAMPP不下载自带MYSQL,配置为原已安装MYSQL方法