Exercise 35
Code
From sys import exit def gold_room (): print "This Room is full of gold. how much do you take? " choice = raw_input ("> ") if "0" in choice or "1" in choice: how_much = int (choice) else: dead ("Man, learn to trpe a number.") &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&NBSP;HOW_MUCH&NBSP;<&NBSP;50: print "Nice, you ' re not greedy, you win! " exit (0) else: dead ("you greedy bastard!") &Nbsp; def bear_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_moved = false while true: choice = raw_input ("> ") if choice == "Take honey": dead ("the bear looks at you then slaps your face off. ") elif choice == "Taunt bear" and not bear_ moved: print "The bear Has moved from the door. you can go through it now. " bear_moved = True elif choice == "Taunt bear" and bear_ Moved: dead ("The bear gets pissed off and chews your leg off. ") elif choice == "Open door" and Bear_moved: gold_room () else: print "I got no idea what that means." def cthulhu_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" in choice: start () elif "Head" in choice: dead ("well that was tasty! ") else: cthulhu_room () def dead (why): print why, "good job!" exit (0) def start (): 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 ("> ") if choice == "left": bear_room () elif choice == "right": cthulhu_room () &Nbsp; else: dead ("you stumble around The room until you starve. ") start ()
Output
Notes:
①exit (0) Interrupt Program
② determines whether the input is a number that can be used with the IsDigit () method of the string
>>> "123". IsDigit () true>>> "abc". ISDIGIT () False
Exercise 36
Notes:
①if sentence Training Rules
Each of the IF statements must contain an else
If an else is meaningless and should not be executed, then use the Die function to alert you
Try to keep only one layer of IF. Although if statements can be nested, try not to nest if, use functions instead of
Treat the IF statement block as a paragraph. Line spacing before, left blank line after paragraph
A Boolean expression should be simple. If it is too complex, assign the operation to the variable beforehand and give the variable a good name
② Cycle Law
Use While-loop only if the expected loop never stops, and in other cases, especially if the number of objects in the loop is fixed or limited, the For-loop loop is used
③debugging's Skills
Use Print to print key key variables and check for errors
Side write script, side run, side debugging
Stupid Methodology Python,lesson 35, 36