这次的习题学习的是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." %bear
elif 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”后异常得以解除。
再小的技能都值得认真对待!!!