- 论坛徽章:
- 0
|
- #Filename:ex35
- from sys import exit
- import re
- def gold_room():#进入黄金屋
- print "这间屋里有100公斤黄金,你想拿走多少?"
-
- next = raw_input("-->")
- if "0" in next or "1" in next or "2" in next or "3" in next or "4" in next or "5" in next or "6" in next or "7" in next or "8" in next or "9" in next :#我实在是找不出别的办法来验证用户的输入了,关键是这里请老师指点一下。
- how_much = int(next)
- else :
- dead("你没上过学吗?数字都不会输入!")
-
- if how_much < 50:
- print "很好,你不是贪心人,你赢得了黄金。"
- exit(0)
- else :
- dead("你是个贪心的混蛋,你输了。")
-
-
- def bear_room():#进入到有熊的房间
- print "这里有一只熊!"
- print "熊手中有一灌蜂蜜。"
- print "胖熊在另一扇门前。"
- print "你如何做才能使熊移动?"
- bear = False
-
- while True:
- next = raw_input("-->")
-
- if next == "take honey":
- print "熊盯着你,并且拍打你的脸。"
- elif next == "taunt bear" and not bear :
- print "熊从门前走开了,你可以通过这扇门。"
- bear = True
- elif next == "taunt bear" and bear:
- dead("这只熊很生气,咬掉了你的一条腿。")
- elif next == "open door" and bear:
- gold_room()
- else :
- print "我不能明白你的意思?"
-
- def cthulhu_room():
- print "嘿,你看见了一个巨大的怪物。"
- print "他盯着你,使你感觉要疯了."
- print "你是选择逃跑还是奉献自己的头."
-
- next =raw_input("-->")
-
- if "fell" in next :
- start()
- elif "head" in next:
- dead("真是美味啊!")
- else:
- cthulhu_room()
- def dead(why):
- print why,"good job!."
- exit(0)
-
- def start():
- print "你进入到一个黑暗的房间。"
- print "这里有左和右两扇门."
- print "你选择哪一边呢?"
-
- next = int(raw_input("-->"))
-
- if next == 1:
- bear_room()
- elif next == 2:
- cthulhu_room()
- else:
- dead("你一直被困在房间里,直到饿死。")
-
- start()
-
-
-
-
复制代码 关键是在验证用户输入的地方,我试过了正则表达式,if re.match(r"%d",next),但不管我怎么输入,都会直接跳到最后一个判断,说我输入的不是数字。没办法之下,只好用了笨办法。不要笑我哦。
|
|