Writing a Python Program - Simple Workflow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is richard white and we're gonna be taking a few moments today to write a Python program not so interested in the actual contents of the program as we are in the workflow that goes with that program writing process so we're not going to be using an integrated development environment or anything like that I'm just gonna be using a terminal today I'm using I term and I'm going to use a very common code writing setup here I'm actually going to split the screen in two on the left side this is where I'll be using vim to actually write my program it's going to be a number guessing program and on the right side I'm going to be running that program so I'll get a chance to type Python have Python run my number guessing program haven't actually written the program yet so it's probably not a surprise that I'm getting an error message there so let's jump in and start writing the program will begin with the usual shebang here and user bin environment if you're not familiar with what these are you will be soon enough I'm also going to put a little doc string in here three quotes and then the name of the program and my name and the date perhaps and another little description maybe of the program we'll go ahead and put all that in here this program has the user guess number between 1 and 100 don't need to do anything more sophisticated than that right now so we'll close out that doc string the three quotes there tell the Python interpreter to ignore this stuff it's just for purposes of the Python doc string or as a comment and then another typical thing that we do we define a main function in a Python program before I get too far into this I'm just going to make sure that everything is working the way I want it to so there's my main function when this program runs as soon as I call the main function here that will tell Python hey go up here to this main function and start going through the list of commands in here right now there's only a single command and execute those commands I'm going to do one more thing here I'm going to you use another little trick here that's very common for Python programs and again if you're not sure what this is then you'll learn about it at some point in the near future so this basically just says if somebody runs this program as a main program then call the main function and go up and then execute these statements here so that's pretty good let's go ahead and write that to a memory we'll save that and then run over here and run that program and see what happens so now when I run the Gesser program it prints out hello world as I thought it would so that's pretty good we've made a good start here let's jump back and start actually creating our real program then so in my main program I guess what I need to do is have the user enter of their guests maybe I'll have a nice little print statement here I suppose the very first thing I need to do is have the program say what's going on I guess a number between 1 and 100 and I'll ask the user for information oh let's have the computer come up with a number right now I'm just going to call it random number and will actually generate that as a random number later on for right now though let's make it the number 35 just for testing purposes and now let me have the user enter their guests and we'll have a variable called user deaths and that's going to get the value and we'll let them enter it just like that and now how are we gonna check to see what happens if user guess is equal to remember there are two equal signs there random number then we'll know they've guessed the number so we can say print you got it and else if they didn't get it what's their current that's not it this is a very rudimentary form of the program we're going to be improving on this quite a bit in a few minutes but for right now let's call that good and test it out let's hop over to the right side and run that program again and see what happens guess a number between 1 and 100 my guess is 50 that's not int and this isn't a very exciting program because most of the time I'm just gonna run it and it's going to tell me I didn't guess the number unless I magically get it one time so it's still a little bit of work we could do here I guess I want to keep going right I want to keep looping through I want to repeat this process of getting a guess until they actually do get the guess they are they get the random number so let me do it let me have a couple things in here first thing I'm gonna do I think is up at the top here I'm going to add this ability I'm going to say that found is equal to false found is going to be a flag variable I'll make a comment about that here flag variable you see if they guessed it the idea is we want to have the program keep track of whether or not they've found the variable if they've guessed it or not and so now that I've got this found variable set what I can do is I can say if not found in other words if they haven't found it yet then do all these things here well then we'll go through this whole process and what I'm going to do is I'm gonna mark this whole block here I need to go down and actually shove this whole block over and so now if I haven't found this thing I'm going to keep on going maybe I can even say would it be appropriate to say while not found do all these things and if they find it then if they do manage to guess it then in addition to saying print you got it I'm also going to set to found equal to true that's a nice idea now we'll end up looping through this block here these statements right here will keep on going through until not found becomes false in other words until it is found let's try that out and see what that looks like we went over to the other side and we'll see if we can run this clear that out and I guess a number of 50 that's not it I didn't write this I need to save this program there we go 50 that's not it 32 that's not it 70 that's not it 35 got it so that's working more or less the way we want it to I guess it still seems kind of random I guess I'd like to give them a hint don't you think if they got it that's okay found will still be equal to else if they haven't found it when they go down here and say you know I'm not just gonna say print that's not it I'm gonna check and say else if and we can actually do that as an else if statement there's a statement called else if L if user guests is greater than the random number then what are we gonna do we're gonna tell them to guess lower else and if it's not equal to and it's not greater than the random number it must be less than the random number we don't even need to check we can just say else and guess higher good well let's see how that works for us here now write that and run over here and guess some number 50s lower 30s higher 37 guest lover 34 guest hi or 35 you got it great functionality this is the basic functionality here it looks like the program is working more or less exactly how we want it to I'm gonna add one more thing in here well I guess I really need to get that random number thing working right up here I've got this random number equals 35 and I need to set that so it actually is a random number every time I run through this I'm gonna pretend for a moment that I don't know anything about random numbers in Python how do you go about researching that kind of thing well we've got a couple of strategies that we can use one is to of course check on the web so it's a good idea to type in Python random number may be random integer since that's what I'm really interested in and those they've got some guesses for me there generate pseudo random numbers if I look through here and I I want to get a random integer here I can skim through and it looks like eventually I'll stumble onto something like random Rand int Rand integer which is looks like I passed it up here maybe a random integer is exactly what I'm looking for I can also search on the page here integer Rand range that looks kind of cool Rand int oh there it is right there that's what I'm looking for so once I learn a little bit about this another good place to learn is if you see any of these URLs that use Stack Overflow Stack Overflow is a great source of answers for quick questions you might even be able to find although we don't want to do this random number guessing game we might even be able to jump in here and steal some code from somebody else guess my number 1 to 100 I'm not going to go there because I want to learn about writing this program but uh so I've got that random R and int and what I'm gonna do now over on the right side is jump into the Python interpreter and check this out the Python interpreter basically allows you to run programs or run statements to execute statements I know I need to import the random module I can do things like print random dot randant between 1 and 10 to see if that works the way I think it's supposed to I need to spell it correctly of course and it looks like that's working kind of the way I want it to it's printing some random numbers for me maybe I can even check it out for I in range 25 will get 1 2 before I'll print out a whole bunch of these numbers and see how that random process works so there's 25 random numbers and you can see it's going from between 1 and 10 all of them so that seems to be working more or less the way I think it's going to the Python interpreter is a great way to check out little functions without actually writing a program you wouldn't write a program in there but you can check out functions real quickly so let me implement that in my program here so I'm going to need to import the random module and then in addition to that instead of this random number here that I kept for debugging purposes let's see I'm gonna go ahead and put a little hashtag there to make that clear that that was that was just something I was working on or debugging now I'm actually going to use my real random statement here random number is equal to random dot brand int 1 to 100 so now it's going to be the real deal let's see what happens go ahead and write that out and run the program and we'll see guess the number guess high or it's already a different number there 67 61 sorry 61 guessed lower took me a few tries to get it but it looks like the program is working more or less the way I intended so there are some other modifications I could make to this program one of the important ones that I'll include right in here right here is to set up a few statements here our comments initialize the program I've tried to use good variable names as I'm going through and so I've got lots of self documentation going on here maybe I'm going to also insert here a comment the guessing process and then at the very bottom down here maybe I'll print out congratulations at the end of it all and congratulations and goodbye thanks for playing our game now in a more extensive program what we probably do is we take some of these little sections the congratulations section or the guessing process section or the initialization section and we put those in their own separate functions and have those called in sequence by the main function but for now I think we're in pretty good shape so I hope this was useful for you thanks for watching
Info
Channel: Richard White
Views: 906,810
Rating: undefined out of 5
Keywords: python, terminal, workflow
Id: pofWfJc3Zog
Channel Id: undefined
Length: 13min 35sec (815 seconds)
Published: Mon Jan 16 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.