Learn Python The Hard Way learning (35)-branches and functions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.