Stupid way to learn Python (36)

Source: Internet
Author: User

Exercise 36: Design and commissioning

Now that you've learned the "if statement," I'll give you some rules for using "for loops" and "while loops," and you'll get into trouble in the future. I will also teach you some debugging tips so that you can find problems with your own programs. Finally, you'll need to design a mini-game similar to the one in the previous section, but the content changes slightly.

Rules for IF statements
    1. Each "If statement" must contain an else.
    2. If this else should never be executed, because it doesn't make any sense, then you have to use a function called die after the Else statement to print out the error message and show it to you, which is similar to the previous exercise, so you can find a lot of errors.
    3. The "if statement" should not be nested more than 2 layers, it is best to keep only 1 layers. This means that if you have another if inside if, you need to move the second if into another function.
    4. Treat the "if statement" as a paragraph, where each if, elif, else combination is the same as the sentence combination of a paragraph. Leave a blank line at the front and back of the combination to make a distinction.
    5. Your Boolean tests should be simple, and if they are complex, you need to put their operations in a variable beforehand and take a good name for the variable.

If you follow the rules above, you will write code that is better than most programmers. Go back to the last exercise and see if I have followed these rules, and if not, correct them.

Warning

Don't be a slave to these rules in everyday programming. In training, you need to use these rules to consolidate the knowledge you have learned, and in practical programming these rules are sometimes stupid. If you think that rule is stupid, don't use it.

Rules of the Loop
    1. Use the while loop only when the loop never stops, which means you may never be able to use it. This is only true in Python, and other languages are different.
    2. Other types of loops use the "for loop", especially if the number of objects in the loop is fixed or limited.
Tips for debugging (Debug)
    1. Do not use "debugger". Debugger the equivalent of a full body scan of the patient. You don't get useful information about something, and you'll find that it outputs an attitude of information that's mostly useless, or just makes you more confused.
    2. The best way to debug your program is to print the key variables in each key link you want to check, so you can check for errors.
    3. Let the program run in part. Don't wait for a long script to finish writing it before you run it. Write a little, run a little, and then modify a little bit.
Home work

Write a game similar to the one in the previous session. The game of any subject of its kind can take one weeks to make it as interesting as possible. As a bonus problem, you can use lists, functions, and mods as much as possible (remember Exercise 13?). And get as many new Python code as possible to get your game up and running.

One thing to note, though, is that you should write out the design of the game first. Before you write the code, you should design a map of the game to create the rooms, monsters, and traps that the player will encounter.

Once the map is done, you can write the code. If you find a problem with the map, adjust the map so that the code and the map match each other.

One last suggestion: Every programmer will be influenced by irrational fears when they start a big new project. In order to avoid this fear, they will stall time and end up doing nothing. I sometimes do that, everyone has this experience, the best way to avoid this situation is to make a list of what you want to do, one time to complete the same.

Start doing it. Make a smaller version of it, expand it to make it bigger, list all the things you want to do, and do it one after the other.

Stupid way to learn Python (36)

Related Article

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.