2. Chapter 1 We are going to learn about programming in general…How to think logically and problem solve. The programming language we will use is Python. This is not meant to be a class in Python, but that is the language we use here…It is free after all. No…Not this kind of python!
3. Chapter 1 Computers needs 2 things to run your code – an editor and an interpreter. The editor is what you use to write the code. The interpreter makes the text into binary code that the machine can understand. Our interpreter is Python. Python comes with an application called IDLE which does both functions. Such a program is known as an IDE – Integrated Development Environment.
4. Chapter 1 Complete the following exercise. We will go over your answers when everyone is finished.
5. Chapter 1 Here’s what the finished code should look like - Now let’s take a closer look at some of the elements of this code…
6. Chapter 1 First, we need to realize that there are variables being used. What’s a variable? Basically, a variable is a label for data. So what are the variables in this code?
7. Chapter 1 Start by going to the Start button, choosing All Programs, scroll down and click on the Python 3.0 folder, and finally, click on the IDLE(Python GUI). The Python shell should open up just like this…
8. Chapter 1 Once the code is entered correctly, the next thing you should do is save your work. Go to the ‘File’ menu and choose ‘Save As’. Name your file game.py. Next, go to the ‘Run’ menu and choose ‘Run Module’.
9. Chapter 1 It would be useful to know more than whether your guess is right or wrong. Like maybe if your guess should be higher or lower.
11. Chapter 1 Print(“You Win!”) Game Over! Guess == 5 Too High! True Guess > 5 True False False Too Low!
12. Chapter 1 Now we need to look at how to write the additional code for the new branch that we have added to the false path… Print(“You Win!”) Game Over! Guess == 5 Guess > 5 Too High! Too Low!
Editor's Notes
The variables are g and guess. The first g assigns the value entered the name g. The int(g) creates a number version of g called guess.We also want to talk about the difference between = and ==. = means ‘set the value to’ and ==means ’are these two things equal?’ This will most always be paired with an ‘If’ statement, which we will discuss more coming up.
Once you have the Python shell open, type in the code just as it appears in the activity we completed earlier. Yes, that includes the indents.
See what happens if you guess low. Once you do this, to take another guess you must run the module again.
What sort of answers did you come up with? The answer for 5 we should already know. Otherwise, we only need to indicate if the guesses are too high or too low.
Let’s think about the code as a roadway. Let’s see how we can add in the directions “Too high” and “Too low”.Look at the first piece…At the intersection you must make a decision…Does your guess = 5? What then would the 2 branches be – One would be true if the answer does equal 5, the other would be false if the answer is not 5.T he answer being 5 or not is the branch condition – or the condition that must be met to choose one of 2 paths in the program – the paths or the true or the false branches. To run the true branch condition the guess must = 5. To run the false branch, the guess must be anything other than 5.So, if you run the true branch what should the result be? Print(“You Win!”)If you run the false branch though, we want to give the user more information to allow them to make a better guess the next time they play the game. How can we do this? Let’s tell the user if their guess is too high or too low so that the next time the program is run they can make a more educated guess. And like with our first branch , we will need a branch condition. What could that be? Guess > 5Once we check our branch condition, we need to set a value for the true or false branch – let’s use ‘Too High’ or ‘Too Low’. With either choice, we get back to the finish, just as we would with the original true branch – ‘Game Over!’