Hello and welcome to Python For Everybody, doing work example. My name's Charles Severance and I'm the instructor for the class. The work example that we're going to work on right now is in Chapter five and it is exercise one. We're going to repeat asking for a number until the word done is entered, and then we're going to print the total. Then, we're going to print the count, and then we're going to print the average at the end. We're going to enter some numbers and I'm going 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'll start my terminal, starts some Atom. So, I've opened the py4e folder and that's cool because now I can do things like say, New Folder, and say I'd like an ex_05_01. Then, go on ex_05_01 and say File, New File, and then say File, Save As and put it in ex_05_01. 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. There I am and I say python3 ex, and there I go. So I'm in good shape. So, there's a couple of things right now and we're going to do the total count and average. So, this is just a basic pattern where we're going to have, we're going to need a iteration variable for the count. I'll call that num, we start that at zero, 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 first string. Remember, input gives us a string, so I'm going to call this sval equals input 'Enter a number' colon space. I'm going to deal with the try and except later, but you can just 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, is for input returns us a string and now I'm going to convert that to float. I'm going to say print fval, so I can print that out. Then, I'm going to do the num equals num plus 1, and tot equals tot plus fval. Now, I do need to deal with the situation where I'm entering the word done. Now, we 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, won't cause us to much problem. If I run Python, let me drag this over here, and I go one, two, three. 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 five, it blew up right here in line five. So, what we want to do is we want to say, one, two, three done, but we wanted to detect that we've 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. Now, print all done. I should be using single quotes here, too much Java coding. Print all done, then I'm going to say print, what do I want to print, the total, the num, and then tot comma num comma tot over num. Now, we've got to be careful because we don't want to divide by zero, but that'll get us sort of a ways. So, this is going to run, this is going to read these things, it's going to accumulate here, this is the accumulator pattern and this is a counter pattern, where we're adding one dual current variable and accumulator pattern, where we're adding a value to it. So, now, we should be able to see the done four, five, six and then done, and the total of four plus five plus six is 15, the number is three and the average is five. So, that's really good, the all done prints out. I just did that for yaks 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, four, five, six. But we do something other than we're done, then we're going to blow up in this float. So, this is where we're going to have to do a try and except because we just know that this line, line seven 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. Then, we're going to have some except code. 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 fval doesn't work, we're not going to see the error message that would be the trace back here on line nine. 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. If I have a problem, I'll print a message out and then I'll say continue. So, the continue basically says go back up 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 dad input four, five, six. Bad input, bad input, done, and I have a total of 15, and three items, and the average is 5.0. So, there we go, that's what we're going to get. That roughly achieves the 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. 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.