Rule 5: Input/Output Controls Storage
The final rule of The Game of Code is how your code interacts with the outside world. Having variables is great, but a program that has only data you’ve typed into the source file isn’t very useful. What you need is input and output.
Input is how you get data into your code from things like files, the keyboard, or the network. You’ve already used open() and input() to do that in the last module. You accessed input every time you opened a file, read the contents, and did something with them. You also used input when you used … input() to ask the user a question.
Output is how you save or transmit the results of your program. Output can be to the screen with print(), to a file with file.write(), or even over a network.
Let’s run dis() on a simple use of input('Yes? ') to see what it does:
1 from dis import dis 2 dis("input('Yes? ')") 3 4 0 LOAD_NAME 0 (input) 5 2 LOAD_CONST 0 ('Yes? ') 6 4 CALL_FUNCTION 1 7 6 RETURN_VALUE
You can see there’s now a new instruction CALL_FUNCTION that implements the function calls you learned about in Exercise 18. When Python sees CALL_FUNCTION, it finds the function that’s been loaded with LOAD_NAME and then jumps to it to run that function’s code. There’s a lot more behind how functions work, but you can think of CALL_FUNCTION as similar to JUMP_ABSOLUTE but to a named place in the instructions.