fromsysimportexit### *gold_room()* 函数,要把获取的字符串,转换成数字,根据数字,然后输出不同结果defgold_room():print"This room is full of gold. How much do you take?"choice=raw_input("> ")# 如果输入2,或其他非0非1数字,程序会执行 dead("Man, learn to type a number.")# 需要引入新的方法,来判断输入的字符串是否为纯数字# Python 的内建功能,提供了一个方法: isdigit()if"0"inchoiceor"1"inchoice:how_much=int(choice)else:dead("Man, learn to type a number.")ifhow_much<50:print"Nice, you're not greedy, you win!"exit(0)else:dead("You greedy bastard!")defbear_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_move 有什么用处,为什么要设定成“False”bear_moved=FalsewhileTrue:choice=raw_input("> ")ifchoice=="take honey":dead("The bear looks at you then slaps your face off.")elifchoice=="taunt bear"andnotbear_moved:print"The bear has moved from the door. You can go through it now."## 又见 bear_move bear_moved=Trueelifchoice=="taunt bear"andbear_moved:dead("The bear gets pissed off and chews your leg off.")elifchoice=="open door"andbear_moved:gold_room()else:print"I got no idea what that means."defcthulhu_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?"choice=raw_input("> ")if"flee"inchoice:start()elif"head"inchoice:dead("Well that was tasty!")else:cthulhu_room()# 想想“DRY”原则,设置 dead() 函数的必要性defdead(why):printwhy,"Good job!"exit(0)defstart():print"You are in a dark room."print"There is a door to your right and left."print"Which one do you take?"choice=raw_input("> ")ifchoice=="left":bear_room()elifchoice=="right":cthulhu_room()else:dead("You stumble around the room until you starve.")start()