Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 2: Guess that number game
Lecture: Getting started with Guess That Number Game

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So let's jump into PyCharm and start building this game. Remember, our convention is that we are going to
0:06 start out with the main thing we run called program, so our new Python file called
0:12 and our app, as the very first thing it had at the top a header so remember we'll do print and we had a bunch of dashes here.
0:19 Now in PyCharm, if you want to duplicate a line or a selection or a set of lines, you can hit command D in OS X or in Windows or Linux control D.
0:29 So I will hit command D a few times, it's a little bit of a shortcut. And then it said guess that number game, something like that,
0:37 let's do some centering, and finally let's do a print, just to leave some space there at the end.
0:43 Before we go any further, let's just run this really quick to make sure everything is working, again, there is no run configuration up here
0:49 because this is a new project, so we'll right click and say run, and then all right, a little header is working fine. Now, the next thing we need to do
0:56 is come up with that random number and the number the computer guesses so I will create a variable called the number
1:03 and now, somehow we need to... here come up with this random number and one of the great things about Python is
1:09 it comes with all of these modules and libraries that we can use one of them is called the random library, or the random module
1:16 Now, PyCharm is not happy, because we need to actually tell Python we intend to use a module that is not what is called the built in
1:26 this is like an extra part of Python so we need to explicitly state I'd like to use this part of Python
1:31 this works identical to whether it's something in Python with the standard Python implementation,
1:36 or something external, like some other library that you've gotten. So we are going to say import, and we just say random, and then we'll say "random."
1:45 now we have a bunch of options, a little warning went away, the one we are interested in is randint we'll come over here and call randint
1:53 and if we hover here for a moment or pause for a moment, it will say it takes two parameters a and b. Now, so I would guess that those are numbers,
2:01 and I'm sure you would as well, and that they somehow specify the lower and upper bound that we are suppose to work with here
2:07 but is this including those n points, somehow between these n points, and we can check the docs,
2:14 but it turns out that this actually includes both n points so what we want is 0 to 100. Let me show you something that PyCharm can do for us
2:22 that really is pretty amazing and one of the things I really like, imagine let's roll this back for a minute,
2:28 imagine that we don't have this import going on, and we don't have this line, so here we are again and we want to say
2:34 just like before the number is equal to random. Now PyCharm has a little red light bulb you can see there and it says,
2:41 Hey, this is not right, but I am pretty sure I can fix it for you, automatically.
2:47 So if I hit "alt enter", it will come up with the list of options to fix it, so I could actually just hit enter and it will say
2:54 we are going to import random and there is a couple of ways we could do it but this top one is what we had done before, so I can just hit that,
3:01 and you could see right at the top it added import, random, now I can type randint and off we go. So, a lot of times when you see an error like this,
3:09 one of these red squiggles and this little light bulb in PyChram that means that there is some kind of auto fix
3:15 it's going to propose for you and it is probably worth checking out. Ok, excellent, so we have our number guessed,
3:23 our number between 0 and 100 inclusive, now the next thing to do is get some input from the user and we are going to just tell the user
3:29 hey, enter a number between 0 and 100 so I'll call this guess, and this is going to come back as text so I'll be very explicit and say guess text
3:36 so we'll say input, just like before, guess a number so guess a number between 0 and 100
3:45 and we'll just let the cursor stop right there, and we'll wait for that. Conceptually, the next thing that we are going to do
3:51 is we want to compare the number against the guess text. Now, Python is not a very forgiving about a quality between numbers and strings,
4:01 these don't really have anything to do with each other, so if we tried to just say print out whether the number
4:06 was... let's say less than the guess text, if I try to run this you will see it'll actually crash,
4:10 so if I put in 42, and it says you cannot sort between or compare the order of integers and strings together, right?.
4:20 You can ask whether they are equal, they will never be equal, but you could ask that question,
4:24 but you cannot do the type of comparisons that we need for this test So the next thing we need to do is
4:30 actually take this guess text and convert it into an integer and we'll call that integer the guess
4:35 so this is super easy, the int class is just built in and we can call it, initialize like this we can pass it some text
4:44 and then what is going to come out is actually a guess so we could say really quickly print the guess text
4:49 and you can actually ask for the type of these so let's print those two out and here we'll print the guess and the type of the guess
4:56 so if we run this two, if I enter 42, you can see, the first line where was guess text is actually a string, "str" is the string class in Python
5:05 we've actually converted successfully to 42 it looks the same when you print it out but it's definitely not the same type in memory.
5:14 All right, the next thing we need to do is write some conditional statements with if and else statements and some comparisons and so on
5:21 in order to determine whether we have won the game the guess was too high, too low, that sort of thing
5:26 this is one of our core concepts from this application so let's go dig into that.

Talk Python's Mastodon Michael Kennedy's Mastodon