Worked Exercise- 3.2دوره: Programming for Everybody (Getting Started with Python) / فصل: Chapter Three- Conditional Code / درس 3
Worked Exercise- 3.2
So let's get started on assignment 3.2, so the nice thing that we can do here is that we can say file, save as, and go up a folder to PY for Everybody. Because to be a programmer, even doing the simplest stuff, you've got to manipulate files, and you've got to be able to use command line. And now I see my new ex_03_02, so I'll say cd ex_03_02 and part of being a programmer is having a lot of folders with wierd names to organize your stuff.
- زمان مطالعه 9 دقیقه
- سطح خیلی سخت
دانلود اپلیکیشن «زوم»
این درس را میتوانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید
متن انگلیسی درس
Hello, welcome to Python for Everybody. I’m Charles Severance, your instructor. And in this short video, we are going to do exercise 3.2, which is really just a rewrite of exercise 3.1. We’re going to deal with some error conditions using try and except. So let’s go ahead and get started, but this is a good example of something where you want to start with the previous assignment. So let’s open the file that I did before, and so this happens to right here, ex 03 01. And there’s my previous program that I wrote for exercise 3.1. And let me set myself up with Python, so I can actually run it. cd Desktop, cd py4e, these will get better, cd ex 03 01 ls. And I can say python3 ex 03 01.py, and it works fine if you give it the right data. But if you give it bad data, 10 and t-e-n, it blows up, right? And like always, you’re getting tracebacks for all kinds of things. But this one’s pretty clear. If you slowly but surely don’t get mad, just say, oh, line 4, what could be wrong in line 4? Could not convert string to float ‘ten’, so what it’s complaining about is on line 4, this variable sr, which is the string t-e-n, the letters t-e-n, is not working. So this function is blowing up, okay? So that’s what it’s complaining about and that’s what we want to deal with. Now when you’re using try and except, you don’t want to just put a try, you want to limit how much data you put in the try and except. So let’s get started on assignment 3.2, so the nice thing that we can do here is that we can say file, save as, and go up a folder to PY for Everybody. Make a new folder called ex 03 02, and then save the file as ex 03 02.py. Okay, so now make sure, get rid of that. So make sure this is different, we’re in a different folder, so we’re not editing that old file. And the first thing I’m going to do is, hey, come on. Well, I’m going to get rid of some of these print statements. I mean, the code’s going to work. Come on. Okay. So the dangerous line of code is right here and so, the problem becomes how are we going to deal with this? And so, let me do this as well. So here is, if I do pwd, I’m in ex 03 01, this could drive you crazy. Oh wait, I’m in the wrong folder, how come this is in the wrong folder? I put ex 03 01, why is this in? See, you’ve got to watch this. I think I’m in the wrong folder. File, open. ex 03 02, ex 03 02. Okay, so now I’m in the right spot. I don’t know why that wasn’t showing up right, but keep your eye on this, especially if your using file names that are so. So, I’m in this ex 03 01, so I’m going to use a command, and you can use this both in Windows and in Linux and in Mac, cd dot, dot. And what that does is that goes up a folder. Part of the goal of this course is to teach you how to use things like this, just how to use these command lines. Because to be a programmer, even doing the simplest stuff, you’ve got to manipulate files, and you’ve got to be able to use command line. You’ve got to use all these things. Not everything is a full screen clickety pointedly thing, okay? And so what does cd do? Well, it looks at this and it goes up one folder, so it moved us up into here. And so you can see after the cd, my current working directory is that, and I can do ls to see the folders. And now I see my new ex 03 02, so I’ll say cd ex 03 02 and part of being a programmer is having a lot of folders with wierd names to organize your stuff. So here we go, now we have this file and this file in the same place, pwd, this matches this, that file matches this name, and I can say python3 ex 03 02.py 10, 10. Okay. So now, I’m just nervous enough to want to double-check this, and so I will do something like this, I will say XYZ Pay, and save it, and then run it again, python3. I don’t care about the numbers. See the XYZ Pay? So now I know I’m in that file, so I do stuff like this all the time to triple-check that I’m really working with the file. So, now I’m in this file and now let me make my mistake again. 10, ten. And now there is my failure, right? So there’s the mistake, and that’s the thing we’ve got to fix. So, we have these lines of code. So what you do in Python when you know that there’s a dangerous set of lines and you want to take out insurance on those. You put them in a try-catch block, a try/except block, and what are we supposed to say when it blows up? It says, error, please enter numeric input, I’ll just copy that and paste this, print “error, please enter numeric input” make that a little bit wider, print that out. And I want to put this print statement back in. Okay? So here we go. So now what’s going to happen, is we’re going to come in here, we’re going to run these. If they work, we’ll skip the except block and continue. If they blow up, then it’s going to run this except block. So let’s run it. Let’s start with the working case, that still works, that’s good. it comes and does this extra print that I’ve got on line 9. Let’s go in the bad case, 10, ten. Okay, so we’re going to parse this a little bit. So it says error, please enter numeric input, but then I still get a traceback. And you’re like, I hate computers, because doesn’t this look beautiful? I got a try and indent it, it looks just like what I saw, right? It’s so beautiful. Why does this computer hate me so badly? Well, then take a step back, and say, you know what? The computer is admitting that it’s confused by what you’ve given it. And so, just look, what line are you mad about, dear computer? Line 9. Okay, let’s take a look at line 9. It’s always this line or the one before it, almost always. Almost always it gets it right. And it says name ‘fr’ is not defined. So let’s just focus on this, it’s complaining about this. And the problem is that it came down here, it run through here, came down here, ran through here, and this blew up, that line never ran. That’s the line that blew up, and then it ran this error. So it never, never, never got a variable or a value in fr. And that’s because, in this particular case, we would either have to put an if statement in to make sure the rest of this code, or we can say, if everything is just so bad in this thing and I don’t want to continue, quit basically says do not continue, okay? So when it comes in here, it runs this, it blows up, it comes down to here, it prints this, and then it quits. And then it doesn’t continue on. So now I can run this again, 10, 10 it works. 10 and t-e-n, and it fails exactly the same way, this part here is exactly the same as this part here. The difference was as soon as we print this out, we quit, so we don’t continue on, okay? So if you’re doing really simple input checking, sanity checking is one of the things we call this, just is the data make sense and don’t continue. If the data doesn’t make sense, if these two statements don’t work, then whatever we’ve been given is nonsensical data that we’re not capable of handling, okay? So that is how to write exercise 3.2. And I hope that you have found this little video useful, and thanks for watching.
مشارکت کنندگان در این صفحه
تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.
🖊 شما نیز میتوانید برای مشارکت در ترجمهی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.