From Test-Scratch-Wiki

**English**

*"Finding the Average of Numbers" redirects here. For other measures of the center of a set, see Finding the Average of Numbers (disambiguation).*

**Finding the mean of numbers** (sometimes called finding the average) is adding up a set of values and dividing by the amount of values. For more information, see the Wikipedia page on it.

## How to do it in Scratch

There is more than one method, however this article will only explore two of them, which are some of the simpler methods.

You must start off with creating four variables:

- A variable that will ultimately contain the answer (called "sum" in this tutorial)
- A variable that will hold the number of values you are calculating the mean of (called "amount" in this tutorial)
- A variable that is used as a repeater (called "iterator" in this tutorial)
- A list that holds the numbers which you are calculating the mean of (called "numbers" in this tutorial)

This tutorial will use the flag to start the script.

Start of by deleting all of "numbers" and setting the three variables to what they need to be.

when green flag clicked delete (all v) of [numbers v] set [iterator v] to [1] set [sum v] to [0] set [amount v] to [0]

You then must ask the user how many numbers they are going to enter, and set that value to "amount." And then you must add all those numbers to the list, "numbers."

ask [How many numbers are you calculating the mean of?] and wait set [amount v] to (answer) repeat (amount) ask [Add a number] and wait add (answer) to [numbers v] end

Then you must add up all the values, and set "sum" to the answer of that. Finally, you must set "sum" to "sum" divided "amount"."

repeat (length of [numbers v]) change [sum v] by (item (iterator) of [numbers v]) change [iterator v] by (1) end set [sum v] to ((sum) / (amount))

There is an alternative way to do this, requiring less scripting and therefore increasing speed. The script is as follows:

ask [How many numbers?] and wait set [amount v] to (answer) delete [all v] of [numbers v] repeat (amount) ask [Enter a number:] and wait change [total v] by (answer) add (answer) to [numbers v] end say ((total) / (amount))

And that is how to find the mean of numbers in Scratch.