Worked Exercise- 5.1دوره: Programming for Everybody (Getting Started with Python) / فصل: Chapter Five- Loops and Iteration / درس 5
Worked Exercise- 5.1
I'll say print Five and I'm going to do this because now I need to get to the point where I'm in the same folder in this terminal window, cd Desktop/py4e/ex, I can string these together. Now we need to write a loop, and I'm going to write this as an infinite loop, while True, with colon, and then I'll indent, and I'll prompt for a string, and remember input gives us a string, so I'm going to call this sval = input. That's what we are going to get and that roughly achieves this same thing and it's a combination of a loop with a exit mechanism.
- زمان مطالعه 8 دقیقه
- سطح خیلی سخت
دانلود اپلیکیشن «زوم»
این درس را میتوانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید
متن انگلیسی درس
Hello and welcome to Python for Everybody. I’m doing a worked example, my name’s Charles Severance and I’m the instructor for the class. And the worked example that we’re going to work on right now is in Chapter 5, and it is Exercise 1. We’re going to repeat, asking for a number until the word done is entered. And then we’re going to print the total, and then we’re going to print the count. And then we’re going to print the average at the end, and we’re going to enter some numbers. And then we’ve got to do some error checking, and we’re going to keep on going. So we’ll ignore this, we’ll just say Invalid input, and then we’re going to ignore it. So I’m going to start from scratch, I’m going to write I’ll start my terminal. I’ll start some Atom. and so I can, I’ve opened the py4e folder. And that’s kind of cool because now I can do things like say, new folder and say I’d like an exercise 05 01. And then go into exercise 05 01 and say File, New File. And then say, File, Save As. And put it in ex 05 01, and then name the file ex 05 01.py. I’m going to start from scratch on this one, instead of adapting another piece of code. I’ll say print Five and I’m going to do this because now I need to get to the point where I’m in the same folder in this terminal window, cd Desktop/py4e/ex, I can string these together. And there I am and I say python3 ex and there I go, okay? So I’m in good shape. So there’s a couple things right now and we’re going to do the total count and average. And so, this is just a basic pattern where we’re going to have, we’re going to need an iteration variable for the count. I’ll call that num, we start that at 0, and then tot, and I’ll start that at 0.0, so that’s the running count and the running total. Now we need to write a loop, and I’m going to write this as an infinite loop, while True, with colon, and then I’ll indent, and I’ll prompt for a string, and remember input gives us a string, so I’m going to call this sval = input. Enter a number, colon, space, I’m going to deal with the try and except later. But you can just kind of know that the floating point value that we’re going to do is, sometimes this little bit of code will fail. I’m just going to take the string value here, sval. This input returns us a string, and now I’m going to convert that to a float. I’m going to say print fval. So I can print that out. And then I’m going to do the num = num + 1, and tot = tot + fval. Now, I do need to deal with the situation where I’m entering the word done. Now we want to check that before we convert it to a float because done, well we can run this. It’s an infinite loop, but it’ll only run a little bit. It won’t cause us too much problem. If I run Python, let me drag this over here, and I go 1, 2, 3, and if I put in something bad, it’s running. I don’t have a way to get out. But you can see that it blew up on line 5. It blew up right here in line 5. So what we want to do is we want it to say 1, 2, 3, done. But we want it to detect that we typed in done, so here we’ll just say if the string value that I got back from input is double equal quote done quote, break. So that basically will break us out. And now, print “ALL DONE”. I should be using single quotes here. Too much Java coding. print ‘ALL DONE’. Then I want to say print, what do I want to print? The total, the num, and then tot, num, tot/num. Now we’ve got to be careful, because we don’t want to divide by 0, but that will get us sort of a ways. So this is going to run, it’s going to read these things. It’s going to accumulate here. This is the accumulator pattern. This is a counter pattern where we’re adding 1 to a current variable and accumulator pattern where we’re adding a value to it. So now we should be able to see the done. 4, 5, and 6, and then done. And the total of 4 plus 5 plus 6 is 15. The number is 3 and the average is 5. So that’s really good, the ALL DONE prints out. I just did that for yucks and you can see the value that’s coming up. So that’s in pretty good shape, so I’m going to comment this out, and comment that out. So this is pretty good, it works just the way we want it to work, 4, 5, 6. But if we do something other than we’re done, then we’re going to blow up in this float. And so this is where we’re going to have to do a try and except. Because we just know that this line, line 7, line 7 is the danger zone, okay? So what we’re going to do is we’re going to put a try in here and then we’re going to indent the part of code that seems strange. And then we’re going to have some except code. And the first thing we have to do in the except code, is print out the word Invalid input. Come back, print, Invalid input. Now, just like in an earlier example, we have to do something here to make sure it doesn’t just keep on going. because if fval doesn’t work, we’re not going to see the error message that would be the traceback here on line 9. We’re going to run here, but we still don’t want to add because fval will be so this is where we can use the continue. So in this code we’re using both the break to say if I’m all done, break. And if I have a problem, I’ll print a message out. And then I’ll say continue, so continue basically says go back to the top, so that is how when we see enter some bad data, we print an Invalid input, and without adding anything new, you don’t really see it here. Without adding anything new, you go back up to the top and enter a second thing. So now, if everything is right, I should be able to type bad input. 4, 5, 6, bad input, bad input, done. And I have a total of 15, and I have three items, and the average is 5.0. So, there we go. That’s what we are going to get and that roughly achieves this same thing and it’s a combination of a loop with a exit mechanism. We have some sanity checking of our input, so making sure that we have some valid input. And we catch it, and we use continue to loop back up to run the next iteration of the loop. And we have an accumulator pattern, and then we can use the accumulated data to print what we want to print. So I hope that this has been useful to you, exercise 5.1 for Python for Everybody.
مشارکت کنندگان در این صفحه
تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.
🖊 شما نیز میتوانید برای مشارکت در ترجمهی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.