Book learn python, the hard EXERCISE, designing and debugging

Source: Internet
Author: User

EXERCISE 36

Designing and debugging

Rules for If-statements

1. Every if-statement must has an else.

2. If this else should never is run because it doesn ' t make sense and then your must use a die

function in the else this prints out a error message and dies, just like we do in the last

Exercise. This would find many errors.

3. Never nest if-statements more than, and always try to do them one deep.

This means if you put a if in a if, then you should is looking to move that second if

into another function.

4. Treat if-statements like paragraphs, where each if, elif, else grouping are like a

Set of sentences. Put blank lines before and after.

5. Your Boolean tests should is simple. If They is complex, move their calculations to variables earlier on your function and use a good name for the variable.

Rules for Loops

1. Use a while-loop only to loop forever, and that means probably never. This is only applies

to Python; Other languages is different.

2. Use a for-loop for all other kinds of looping, especially if there is a fi xed or Limited

Number of things to loop over.

Tips for debugging

1. Do not use a "debugger." A debugger is like doing a full-body scan on a sick person. You

Don't get any specific useful information, and you find a whole lot of information that

Doesn ' t help and is just confusing.

2. The best-of-the-to-debug a program is-use print-to-print out the values of variables at

Points in the program to see where they go wrong.

3. Make sure parts of the your programs work as your work on them. Do not write massive files

of code before you try to run them. Code a little, run a little, fix a little.

One final word of advice:all programmers become paralyzed by irrational fear starting a new

Large project. They procrastinate to avoid confronting this fear and end up not getting their program working or even started. I do this. Everyone does this. The best of the avoid

A list of things you should does and then does them one at a time.

Just start doing it, do a small version, make it bigger, keep a list of things to does, and do them.

Example:

From sys import exit

Def dead (why):

Print why, "good job!"

Exit (0)

If Next = = "Left":

Bear_room ()

Elif next = "Right":

Cthulu_room ()

Else

Dead ("You stumble around the guest until you starve.")

Book learn python, the hard EXERCISE, designing and debugging

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.