We have already learned the if, function, list, and so on. Now let's change our thinking and see if you can understand the following code?
[Python]
From sys import exit
Def gold_room ():
Print "This room is full of fold. How much do you take? "
Next = raw_input ("> ")
If "0" in next or "1" in next:
How_much = int (next)
Else:
Dead ("Man, learn to type a number .")
If how_much <50:
Print "Nice, you are not greedy, you win! "
Exit (0)
Else:
Dead ("you greedy bastard! ")
Def bear_room ():
Print "There is a bear here ."
Print "The bear has a bunch of honey ."
Print "The fat bear is in front if another door ."
Print "How are you going to move the bear? "
Bear_moved = False
While True:
Next = raw_input ("> ")
If next = "take honey ":
Dead ("The bear looks at you then slaps our face off .")
Elif next = "taunt bear" and not bear_moved:
Print "The bear has moved from the door. you can go through it now ."
Bear_moved = True
Elif next = "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 edevil Cthulhu ."
Print "He, it, whatever stares at you and you go insane ."
Print "Do you flee for your life or eat your head? "
Next = raw_input ("> ")
If "flee" in next:
Start ()
Elif "head" in next:
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? "
Next = raw_input ("> ")
If next = "left ":
Bear_room ()
Elif next = "right ":
Cthulhu_room ()
Else:
Dead ("You stumble around the room until you starve .")
Start ()
Running result
Root @ he-desktop :~ /Mystuff # python ex35.py
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> Right
Here you see the great edevil Cthulhu.
He, it, whatever stares at you and you go insane.
Do you flee for your life or eat your head?
> Ss
Here you see the great edevil Cthulhu.
He, it, whatever stares at you and you go insane.
Do you flee for your life or eat your head?
> Flee
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> Left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front if another door.
How are you going to move the bear?
> Taunt bear
The bear has moved from the door. you can go through it now.
> Taunt bear
I got no idea what that means.
> Open door
This room is full of fold. How much do you take?
> 4
Man, learn to type a number. Good job!
Root @ he-desktop :~ /Mystuff # python ex35.py
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> Left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front if another door.
How are you going to move the bear?
> Taunt bear
The bear has moved from the door. you can go through it now.
> Open door
This room is full of fold. How much do you take?
> 1
Nice, you are not greedy, you win!
Extra score exercise
1. Draw a game map to see how you are playing.
2. Modify your mistakes, including spelling errors.
3. write comments for functions. Do you still remember the doc document?
The first line of comment in the function is included in.
4. Expand your game. Can you simply expand it?
5. There is a bad method in gold_room to judge if you entered a number. can you improve it in a better way? Refer to the int () method.
If nexe. isdigit ():
Author: lixiang0522