Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.7 Building an Average Calculator

Now that we have a firm grasp of the control of the flow of logic in our code, let's create a new application. This time we are going to create a simple application that allows the end user to input an arbitrary amount of numbers and returns the average of the numbers. Oh, and we will encounter a gotcha or two along the way!

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


3.7 Building an Average Calculator

Now that we've learned to control the flow of our application with a lot of nice constructs of the language of Python. Let's go ahead and start to actually create an application, a very simple application that could really serve a purpose. A very general purpose of saying taking in some arbitrary number of numbers from the user and then calculating the average of those numbers. Now there could be functions out there already pre-written to do this, but this will be a good exercise to be able to use functions and for loops and if else statements and while loops and all sorts of nice little things we've been learning over the last several lessons. So let's go ahead and get started. So you can see here I've created an average app.py file. And hopefully that is not the quality of the application and actually just kind of a name to specify the purpose of the application is to calculate the average of a given set of numbers. So let's go ahead and see how we can start something like this. The first thing I tend to do when I structure my applications is I start to think about what is the process that I'm going to need to do, the interaction with the end user? And then ultimately, how my going to do something with that data? So let's go ahead and start by creating the part of the application that's going to take in the data from the end user, since we haven't really talked about getting input from the user in a few lessons. So let's go ahead and refresh our memory here. So what I wanna do is, I wanna take in some sort of arbitrary number of values from the end user. And I want them to be numbers, because like I said, I want this to calculate the average of a given set of numbers. So let's go ahead and create a list, we'll call it numbers, and it's gonna be empty to start with. There's not gonna be anything in it. Then we also want to create a loop here that's going to ultimately just run forever, until maybe we get some sort of indicator from the user that that is the end of getting the inputs. So let's go ahead and create a while loop. And this is going to seem a little strange at the beginning because what we're gonna do is, the condition that we're going to specify for our while loop iis while(True). Now this may seem a little odd. And you may think to yourself, well this is just gonna run forever, why would I ever want to do something like this? Well I'm gonna show you. So what I want to do here is I want to have this loop run forever until we get some sort of signal from the end user to stop. So, a way to do that is typically to create an infinite loop or what would look like an infinite loop. And then check for conditions within that loop. And then jump out of it or break out of it like we talked about in a previous lesson. So we want to continue to get values from the end user, so I'll say val = input. So we're gonna get input from the user. We're going to say please enter a number. And like I said, we want to get some sort of indicator from the end user that they are done entering numbers. So we're going to put in a little got you here, a little signal to the user to say, q to quit. So what we want them to do is, we want them to enter in consistently or continually numbers that we're going to keep track of. And then when they're all done we want to specify that is the end user by typing in the letter q, which means we are ready to quit. Okay great. So what do we have to do now? Well, the first thing that we should obviously do, is we should be checking to see if this value is q. Because if this value is q then we're all done, and we want to jump out of our loop or we want to break out of our loop. So let's go ahead and check for that now. So we use our if statement. And will say if val is not equal to, or you can say equal to depending on how you want to structure your application. I like to typically start with the positive case, but a lot of people like to start with the negative and that's fine either way. So positive case in that if they have not specified a q, so we'll say this is not equal to q. Then we want to append that value that they're passing into our numbers list. And remember, all the inputs that we're getting from the input function are strings. So we need to use our int function and cast that value to an integer and then ultimately save it into our numbers list. Great, then we would want to keep going and keep processing our loop. But if the value is equal to q, which would be the else statement here, because we're only checking to see If it's equal to q or not equal to q. If it is equal to q in this case, then we want to jump out of our while loop and we're going to do that in the form of a break statement. So we'll go ahead and break there. So now at this point, we have created our number list. We've started an infinite loop or at least what looks to be an infinite loop. And we're going to continuously get input from the end user until we hit q, and then we're going to quit. So once we have actually finished running through this loop, let's go ahead and jump down here. And just to show that we have come out, we'll say print, the user entered, and we'll grab the length of the numbers list. So the user entered some number values. Just like that, we'll just concatenate that string together. So let's see if this actually works. So we'll say python average app. Please enter a number or q to quit. So let's go ahead say 1, 5 or 56 or 0 or whatever, 3, and then hit q. So we hit enter now and we see the user entered four values. We entered 1, 56, 0, and 3, and we're not counting that q, because remember If the value is not equal to q, we're appending it to the list. If it is equal to q, then we're just breaking out. So that last one is not being counted, it's just four values, the first four actual integer values we've passed in. Okay, so that's pretty good we've gotten this far. We can now get an arbitrary number of values or numbers from the end user that we want to then ultimately process and get the average of those values. So how are we gonna do that? Well, because we are trying to think in such a way that we would like to structure our application so that it is reusable, or maybe other people might benefit from the code that we're writing. We may want to think about taking this functionality of calculating an average of a list of numbers that are passed in it, maybe think of that as a function. So typically when we're creating applications and I'm writing functions, I like to define them at the top so that they're all together. So we're gonna come up to the top here and we're going to define a function. And we'll call this function, get_average. And then, it's going to take in a number list, so this is going to be our number list, and then we're going to specify the body of our function. So what are we going to do here? Well, the first thing we need to think about is how do you actually calculate the average? Well you sum all the values provided and you divide it by the number of values that are provided. So those are two values that we're gonna need to take into consideration, the total or the sum of all the values, as well as the length of the number of values. So let's go ahead and start off by saying, let's create the total. And let's just initialize that to be equal to 0, because later on, I'm gonna want to increment this. And if I don't specify it to have a value in the beginning, then we may get some strange results. So we're gonna instantiate this and give it kind of an inferred type of an integer value. And then we want to get the list length, which we have done before. We're just simply going to get the length of our number list, just like that. So now we have the running total. And we have the length of the list that's been passed in. All right, so now what do we want to do? Well, basically what we want to do, is we want to loop through the number list. And we want to continue to get a running total of all the values there and then, ultimately return the value of total divided by list_len. So, let's go ahead and start there and we'll just see how that works out. So, we'll say for each number, In our number list. So for each one of those values stored in the num variable, I want to increment to the total by that num value. So we'll say total += num. So that's going to continually increment that running total, and then we want to return total / list_len. So let's go ahead and say that. So if we take a look at this, this seems fairly decent. We have our get_average function here, that's going to get a running total of all the values. It's going to return the total divided by list_len. So let's go ahead and get that value down here. So instead of printing this line here, let's just create a variable called average or avg. And we'll set that equal to get_average. And then we will pass into it the numbers that we are accumulating from the end user. And then we'll finish off by, we'll just put a little print here, so we give ourselves a little space on the shell. And then we'll print off, the average of the numbers is avg, just like that. So let's go ahead and save that. Now let's go ahead and clear off our shell here and see how this works. So we'll run our application. It says, please enter a number. So we'll say 1, 4, 9, and 12. So that once we get there, maybe that's enough, so we'll go ahead and type in q. And we say the average of the numbers is 6.5, and I'm fairly certain that if you go ahead and punch that into a calculator, that will be accurate. So that's a good thing, but I can definitely see a problem here. And if you have ever done any sort of development prior to this, you probably see the same thing, but let's go ahead and talk this through. Now, the basic operations that we're doing here in our get_average are we want to get this running total. And the problem I'm thinking about might come in this line here. So let's go ahead and give another case here, maybe a test case you might want to call it. Let's go ahead and run our application. Let's say, what happens if I were to type in q at the very beginning and hit Enter? Oh boy, we have a big problem here. We have all sorts of errors here. Well, the problem that is most glaring here is our 0 division error. We cannot divide anything by 0, and the problem here is that at the time when the user entered q to start here, this numbers is an empty list. So its length here is 0, so we never got a valid value. We never hit this line of code. We simply hit this break statement right away. Then we passed into get_average, our empty list here. So once we come up here, total's equal to 0. List length is going to be equal to 0. We're never going to do any operations within our for loop, and we're ultimately going to return 0 divided by 0, which is a big no no. So in order to get around this, we should be doing a check before our for loop. And we'll say, if list_len == 0 then we'll just basically return say total. We'll just return some sort of arbitrary number, which is ultimately in this case going to be 0. So let's go ahead and try this again. So now if we say, please enter a number, q to quit, we type in q. The average of the numbers is 0 because we're just assuming that the input is 0. So there you have it. We've now built a very simple application that is now able to interact with the end user an indiscriminate amount of times, until the user decides he or she is done entering numbers. And then once they have entered in those numbers, we then use a reusable function that we can reuse later on in our programming career, or share with some of our colleagues, or other people that might be doing similar operations. And this is one of the main reasons why we ultimately create these functions, is to create reusable code. Now I don't have to copy and paste this block of code all over my application. I can simply refer to get_average wherever I need to and I'm going to have that functionality at a moment's notice. So there you go, a very simple application that takes into account and utilizes some of the nice tools we've been working on throughout this section. Namely functions, if statements, for loops, while loops and break statements. So there you have it. So now that we have all of this in our toolbox, and we're really starting to pick up some momentum. In the next lesson we're going to dig into some more interesting data structures that you might find yourself needing, or really wanting to use as your applications begin to get a little bit more complex.

Back to the top