To help you understand your first game you will write a smaller program first. Now you will work in the editor window and learn how to save your file. Open the editor window, not the shell window, and type:
Save your program and give it a name, but add .py after the name you choose. Then close Python. Reopen IDLE and go to file, then recent files, and choose your program name. Go to the run button at the top of the edit window and choose “Run module.” When it asks you, “How many eggs can you eat?” type a number or a word.
If your program didn’t work, go back and make sure everything is typed exactly as I did typed it, and make sure you did typed in the right windows. Before we go on to your first mini game I will explain more about the program you just made.
Change the lines above and make up your own mini programs to practice and see what happens. When you are ready, go on to the next part and make your first mini game.
As a programmer you will often find yourself needing to use a statement based on a condition. If you eat 2000 candy bars: you will get sick. This kind of conditional statement is called an “if” statement.
We will use the if and the elif statements to make your first simple game. Let’s name your game; “Fred; The Middle Aged Alien Chicken.”
To start programming I will make a note of the name of my game at the top of the window as a reminder using the # key.
Make sure you are in the right window and that you type everything exactly as you see above. Then click on run and run module to try it. Enter a number and it will give you a reply. Enter another number and ...ahh!! it just repeats what you entered! You close the game and run it again to try again. So, at this point, you can only make one guess before the game replies and stops working. Don’t worry; we will fix that in a moment when you learn about loops. For now, I will explain the code we have so far:
Remember pages ago when we talked about blocks? You were younger then, but maybe you remember. Well, we need to review that stuff and expand on the meaning to be able to teach you about loops and how to fix our games problem of stopping after one guess.
If you haven’t guessed it, a loop is when the computer goes back and repeats something in a cycle until something breaks it out of that cycle. The while statement tells the computer to continue looping while some condition is continuing to happen; usually while it is still true.
As long as some condition is true in a while statement, a block of statements or code will be repeated. It will break out of that loop when the condition is no longer true for that block of code.
Most of this code was already explained, so we will just look at the changes and the new things. You didn’t need to type any of the red comments for your code to work. These were just there to help you. The spaces are also ignored by the computer and are placed in code to make it easier for humans to read. The indentations are important changes. We will discuss those in a moment. Now, if you try the game, you can continue guessing until you guess correctly. The game will only end when you guess the correct answer. Try it.
Whew! Now you can understand how Python can save you time. Look at how many English words it just took to explain what we were doing. Then compare that to how short the Python blocks of code are: