Chapter 99 Introduction to Python Interactive Programming

www.codesculptor.org

1. To understand programming in the idea of a game, it is a series of rules that can form an encapsulated function to achieve a certain input and output construction correspondence. In fact, the game is a complex function that has a certain output (e.g. score, game over, etc.) for the player's input. Luckily, we don't have to start from scratch, and we can do it quickly using a variety of libraries written by our predecessors.

2. Project thinking, decomposing complex goals into small modules and even functions (in the form of files). As a result, you can eventually develop a variety of games and even software. With the characteristics of Python's object-oriented programming, we need to define classes and functions one by one, and theoretically various variables correspond to a property of reality, as long as there are enough properties described, we can approximate real things with higher accuracy. Object-oriented, based on classes and objects, is property + method, property is the definition of different variables (describing different properties), methods are various defined functions. In essence, an object is an abstraction of reality, and an object is an abstraction of an instance, and an instance is a concrete representation of an object. For example, we all live in a house (object), but the specific location, size, etc. (instance) are different. Simple, small projects don't need templates, but implementing complex functions can get the job done better with templates

3. The program needs to realize its function in a certain operating environment. Programs are essentially based on the interaction of text symbols, and the lower level is the operation of various logic gates, and we use various definitions such as ASCII codes to construct the relationship between symbols and binary numbers, so that we can interact with the computer through text input. The same is true for today's graphical interface interactions. So we can use these measures to achieve functions that go beyond language, such as our various apps.

4 Expressions are formed as simple combinations of symbols (basic arithmetic operators include addition, subtraction, multiplication, division, and multiplication), and the operation symbols have a certain priority. Then on this basis, functions can be formed, and the functions of variables correspond to the definition domain of mathematics, and the output of the function corresponds to the value range. These basic data types such as integer int, float, string, etc., are the types of these variables. The so-called cast conversion is to change the scope of application of the data it stores, such as converting an integer to a floating-point number is a change in precision (anything beyond the 15-digit decimal place is discarded)

5. Variable naming is important (valid variable names in Python are composed of letters, numbers and underscores, variable names must start with letters or underscores, variable names are case-sensitive), on the one hand, this is the unit that stores data (the placeholder used to store the "value"), and on the other hand, the parameters of the function need to have a clear meaning to make it easier for people to understand. In fact, depositing values in variables avoids having to calculate the same expression over and over again, which makes sense in our math calculations. The variable name should help you understand the meaning of the value that the variable represents. Then the variable needs to be assigned, generally speaking, it is a good habit to initialize, and then update it to put it into a certain expression to operate, and finally achieve a certain output. Of course, we need to get into the habit of testing, i.e. thinking about what the correct answer should be before running the program, so as to provide a basis for code changes (using the print statement). Also, it's important to save your program in a timely manner, always remembering Ctrl+S.

6 A simple program is a line-by-line input statement, and the statement is executed immediately after running the program, which is a basic measure, but it is very cumbersome, and we need to further abstract it. This is the function, which reduces the repetition of operations like the value of a variable, and this architecture is actually code, but only when you call this function, the code inside the function will be executed. Defining a piece of code once can be called multiple times, which greatly simplifies calculations. Clearly writing the usefulness of a function into a comment helps to integrate a whole set of functions. Function header: def function name (parameter): Function body: specific statement such as calculation formula, and then have a certain output or return. Functions are executed only when they are called. In fact, functions can be regarded as black boxes, and we know that they can achieve certain outputs for certain inputs, which can eventually be upgraded to the level of algorithms and even various functions.

7 How the code works, combined with the von Neumann computer system, we know that this is the selective extraction of data from different blocks of memory to CPU computing (based on variables, which are stored at a certain address on the hard disk), and then various return values are also stored in a specific memory address.

8 mode operation mod, is the remainder of the dividend, a=b*c+d, amodc=d. can be used to calculate the hours of 12-hour or 24-hour system, such as 20:00 and then 8 hours is a matter of what point, you can use 20 plus 8 to get 28, you can take the remainder of 24 to get 4. Modulo arithmetic and remainders can be used to calculate the hours of a 24-hour clock. This is a very important way to calculate cycles, such as when a 2D game object can move around, and when an object moves to the edge of the interface, sometimes it is desirable that it can move around the screen, disappear on one side and pop up on the other. The value of its position can be changed in this case using modulo arithmetic, where the new position value is equal to the original position value plus the move value and then the remainder of the screen width is obtained.

How to deal with strings and numbers, i.e. coercion of data types, such as numeric value to string output, can use the str function to convert numbers to strings, enabling the use of different operations (the + of the number is the calculation, and the + of the string is the concatenation string)

Some of the built-in functions that call Python are set up, and the set of functions outside of Python is the module, which is built by someone else and added to Python, and in order to call these functions, you need to import them into the importsimplegui.

9. The essence of a computer is logical operations. Any complex program can be broken down into basic Boolean logic, including NOT (NOT), AND (AND), and OR (OR). The continuous combination and nesting of these basic logic gates can form complex functions.

10. The control flow of the program, from the basic sequential execution to the combination of sequential branch loop statements (the control flow can be changed according to the value in the program), and then to a variety of complex stacking structures. In Python, the way to change the control flow based on data is to use an if statement, which can have multiple conditions connected by AND or NOT. The ability to use arbitrary Boolean logic and assertions allows for powerful and flexible behavior. When examining the assertion of a function, the function name should start with is.

11. Test and debug debugging. To get an error message, to understand it and fix it, you need to learn some detection skills. For example, when we write the wrong function name or variable name, we get a name error message because they are not defined. Misspelling the part after the full stop will result in a property error. Every function header, "if" and "else", ends with a colon. Note the difference between global and local variables.

Coding style and readability: Good naming (variables make sense) and make sure the naming is consistent, put relevant information into docstrings, and make clear comments. Simplify conditions and other content in a variety of ways to produce code that is easier to understand. Add blank lines between definitions and spaces between expressions for more readability.

12. The control flow of the program depends on the program structure as well as the specific inputs, which is a process-oriented approach. An event-driven programming model can be used to build interactive applications. Different blocks wait for different inputs as response conditions, and then run the handler's function, and the function block continues to wait after the handler runs. As a result, the number of combinations processed by these functions has exploded exponentially, allowing for the flexibility to handle a variety of situations. This is consistent with the idea of combining sequential branching loop statements. Finally, some kind of event is used to trigger the exit of the program, that is, the shutdown.

13 There are many types of events, depending on the type of system you run and what interests you: input events (button or text box input), keyboard events (key press or key lift events), mouse events (click or drag events), and timer events (periodically triggered). Write a handler for one event, but not for all the same event. Most of the time the system waits for an event to happen. There is a periodic triggering of a timer event to avoid waiting for the user's action. We use a timer to trigger, and then we design the action to trigger.

Event queues: As soon as an event occurs, the system places them in a queue. While the program is waiting, the system looks at the event queue and runs until the event queue is empty. The program should be written in response to events and act accordingly, regardless of the order in which they occur. Handlers are exclusive when they are running, and if there are new events, they are simply queued internally (without triggering the corresponding handler), so that if there is an endless loop in it, all other events are blocked.

14 Programming model: Write the handlers first - register them - respond to the event and be executed - the program ends. When the system is running, the program waits most of the time, and when an event occurs, it is first put into the event queue, and then the system extracts one from the event queue in turn and runs the corresponding handler. The programmer does not decide when a function is called and sent, but rather the system calls the handler function when an event occurs. All we know is that if an event occurs, a specific handler will run. This is the underlying thinking behind the writing of graphical user interfaces, which is event-driven programming, where event handlers respond to events.

15 In an event-driven program, different handlers need to share a common set of information for an interactive program to run: let all handlers work together, i.e., let them share a set of global variables.

Global variables (defining a variable outside a function, it is not possible to modify a global variable inside a function unless the variable is declared global; using global variables is the easiest way to handle data communication between various functions, and can also hide information and control the visibility of information) and local variables (variables created inside a function can only be modified and used inside the function, and the local variable will no longer exist after the function is called. Local variables should be used in preference to them)

16SimpleGUI program structure: 1 Define global variables, which are basically the state of the program, 2 Define any helper functions required by the program to help the event handler accomplish a specific task, 3 Define the classes that the program needs, 4 Define the event handler, which can use the global variables, helper functions and defined classes you define in the program, 5 Create a Frame, 6 Register all the event handlers we need, 7 Start the frame and the timer in the program.

17. Interactive applications (such as the definition of the same kind, which need to have basic properties (variables that store data) and operations): first need to build a certain framework, then determine the corresponding control area (create buttons, input boxes, etc.), iteratively develop, continuously add the required functions, and finally form a whole with certain functions.

18How computers work and how they organize the drawing process: the display displays a certain visual output based on what the computer makes, with a resolution corresponding to a two-dimensional grid of pixels, which is logically stored in the frame buffer (a data structure that tracks the values of the pixels you see on the screen); The refresh rate indicates how often the computer displays the contents of the frame cache on the actual screen. Each app or screen registers itself with a special function for a paint handler.

String processing, in most cases, is the printing of strings to the console. Computer science counts from scratch. Complex conditional statements can be written to construct strings that fit the program