Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 2: Guess that number game
Lecture: Using loops and conditionals
0:00 So let's write the test for our code to see if the guess that the user entered is correct.
0:06 So what we want to do is want to say if, and then write a conditional comparison statement, a boolean statement,
0:13 so we can say if the guess, not the text remember, the actual number is less than the number, then we want to print something out
0:21 and for defining these if statements and all sort of code blocks into Python we use a colon to indicate we are about to define a block of code
0:30 that is going to run in some situation maybe it's a function, maybe it's a loop, in this case, it's a block in our if statement.
0:36 So when it's true the guess is less than a number, we are going to print out something like too low, the guess is too low, the guess is lower
0:44 than the number the computer came up with. So that is one part, next, we are going to ask else if it's the case so we'll say elif again,
0:53 if the guess is too high, if it's higher than a number we'll print too high, like so, and notice, when I hit enter,
1:01 when I type this block, I will just go back for a second, if I type x and hit enter, or complex or whatever, PyCharm wants to correct it for us
1:09 and I say test and I hit enter, it goes back to the same line here, but because PyCharm knows about the structure with colons,
1:15 if I go and I type something like if x: enter, it automatically indents for me, so you will see these Python intelligent editors like PyCharm
1:25 make it really easy to write code correctly. So we have if, indent, print too low, else if "elif" guess is too high, print too high,
1:34 and the last thing I want to do is say else: and we'll print you win. So there is a few more things we are going to need to do
1:42 to make this work just the way we'd like but let's go and run it and see how it's working. Ahh what's a good guess?, 54 it must be 54, too high,
1:50 oh, well the way game is supposed to work it's supposed to say too high, now you've got another chance.
1:56 So what we actually need to do is go around and around, and that means we need to find something called a loop,
2:01 and the type of loop that work really well for this situation, in Python is something called a while loop.
2:07 So you say while, and then some sort of condition goes here that you want to test, and as long as that is true,
2:12 you are going to go around and around and around, so we want to say long is the guess is not a winning guess, we want to keep going around
2:18 so we'll say guess, you say equals like this, remember, and if you say not equals you say exclamation equals != So now we'll say colon,
2:27 we need this whole if statement/else if part to run, while this loop is going, and the way that we do that in Python
2:36 is we indent it so I can highlight in PyCharm and hit tab and it is going to indent everything four spaces for us.
2:42 And then when we are no longer indented, if I print something down here, done like so, you can see this is actually out of the loop.
2:50 Now, if I run this something really bad is going to happen, we are going to end up in what is called an infinite loop,
2:56 because, we get one piece of input here from the person we convert it to a guess let's say it's 52, who knows, whatever, and when we go in here
3:05 and unless you guess the winning number, it is going to go around and around and around
3:09 either say too low, too low, too low, or too high too high, too high, and it's never going to give the user another chance to enter this text.
3:15 So what we need to do is make sure that we ask this question, and do the conversion within the loop. Ok, so remember intend, so it's part of the loop,
3:23 we'll talk more about that in a moment, so when it get the number, and then we are going to convert it and so on.
3:28 Now, you can see PyCharm has this sort of highlighted as an error and it says guess cannot be defined after it's used.
3:36 So basically we need to give some kind of value for the initial test so we get into this loop at least once so we can pick something like negative one
3:44 or something that will never be a winning guess, that will make sure that we at least run to this scenario one time, now let's try it. 54- too high,
3:53 40- too low, 45- too high, 43- do I feel lucky- YES!!!, I was lucky, awesome. So, that is how the game works, we can do a little bit nicer stuff
4:05 but let's take a moment and pause and look at this concept of this indentation and defining code blocks that run by using indentation.
4:12 This is one of our core concepts that we are introducing as part of this app.