1 from sys import exit 2 3 def gold_room(): 4 print "This room is full of gold. How much do you take?" 5 6 next = raw_input("> ") 7 if "0" in next or "1" in next: 8 how_much = int(next) 9 else:10 dead("Man, learn to type a number.")11 12 if how_much < 50:13 print "Nice, you're not greedy, you win!"14 exit(0)15 else:16 dead("You greedy bastard!")17 18 19 def bear_room():20 print "There is a bear here."21 print "The bear has a bunch of honey."22 print "The fat bear is in front of another door."23 print "How are you going to move the bear?"24 bear_moved = False25 26 while True:27 next = raw_input("> ")28 29 if next == "take honey":30 dead("The bear looks at you then slaps your face off.")31 elif next == "taunt bear" and not bear_moved:32 print "The bear has moved from the door. You can go through it now."33 bear_moved = True34 elif next == "taunt bear" and bear_moved:35 dead("The bear gets pissed off and chews your leg off.")36 elif next == "open door" and bear_moved:37 gold_room()38 else:39 print "I got no idea what that means."40 41 42 def cthulhu_room():43 print "Here you see the great evil Cthulhu."44 print "He, it, whatever stares at you and you go insane."45 print "Do you flee for your life or eat your head?"46 47 next = raw_input("> ")48 49 if "flee" in next:50 start()51 elif "head" in next:52 dead("Well that was tasty!")53 else:54 cthulhu_room()55 56 57 def dead(why):58 print why, "Good job!"59 exit(0)60 61 def start():62 print "You are in a dark room."63 print "There is a door to your right and left."64 print "Which one do you take?"65 66 next = raw_input("> ")67 68 if next == "left":69 bear_room()70 elif next == "right":71 cthulhu_room()72 else:73 dead("You stumble around the room until you starve.")74 75 76 start()
类似迷宫开门游戏,通过不断输入关键字进入最终的gold_room。
疑问:
1、int()起到的用途
Help on class int in module __builtin__:class int(object) | int(x=0) -> int or long | int(x, base=10) -> int or long | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is floating point, the conversion truncates towards zer | If x is outside the integer range, the function returns a long instead. | | If x is not a number or if base is given, then x must be a string or | Unicode object representing an integer literal in the given base. The | literal can be preceded by '+' or '-' and be surrounded by whitespace. | The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to | interpret the base from the string as an integer literal. | >>> int('0b100', base=0) | 4 | | Methods defined here: | | __abs__(...) | x.__abs__() <==> abs(x) | | __add__(...) | x.__add__(y) <==> x+y | | __and__(...) | x.__and__(y) <==> x&y | | __cmp__(...) | x.__cmp__(y) <==> cmp(x,y) | | __coerce__(...) | x.__coerce__(y) <==> coerce(x, y) | | __div__(...) | x.__div__(y) <==> x/y | | __divmod__(...)
2、dead()的用途:就是死亡的意思= =
3、start()用途:开始的意思= =