Home > other >  Python Find the mean school assignment - What is a loop?
Python Find the mean school assignment - What is a loop?

Time:03-29

I have been working on this assignment for about 2 weeks and have nothing done. I am a starter at coding and my teacher is really not helping me with it. She redirects me to her videos that I have to learn from every time and will not directly tell or help me on how I can do it. Here are the instructions to the assignment (said in a video, but made it into text.

Find the mean Create a program that finds the mean of a list of numbers. Iterate through it, and instead of printing each item, you want to add them together. Create a new variable inside of that, that takes the grand total when you add things together, And then you have to divide it by the length of your array, for python/java script you’ll need to use the method that lets you know the length of your list. Bonus point for kids who can find the median, to do that you need to sort your list and then you need to remove items from the right and the left until you only have one left All you’re doing is you need to create a variable that is your list Create another variable that is a empty one at the moment and be a number Iterate through your list and add each of the numbers to the variable you created Then divide the number by the number of items that you had in the list.

Here's what I've done so far.

num = [1, 2, 3, 4, 5, 6];
total = 0;

total = (num[0]   total)
total = (num[1]   total)
total = (num[2]   total)
total = (num[3]   total)
total = (num[4]   total)
total = (num[5]   total)

print(total)

However, she tells me I need to shorten down the total = (num[_] total) parts into a loop. Here is how she is telling me to do a loop in one of her videos.

for x in ____: print(x) or for x in range(len(___)): print (x 1, ____[x])

there is also

while i < len(___):
print(___[i])
i = i   1

I really don't understand how to do this so explain it like I'm a total noob.

CodePudding user response:

First of all, loops in python are of two types.

  • while: a while loop executes the code in a body until a condition is true. For example:

     i = 0
     while(i < 5):
         i = i   1
    

    executes i = i 1 until i < 5 is true, meaning that when i will be equal to 5 the loop will terminate because its condition becomes false.

  • for: a for loop in python iterates over the items of any sequence, from the first to the last, and execute its body at each iteration.

Note: in both cases, by loop body I mean the indented code, in the example above the body is i = i 5.

Iterating over a list. You can iterate over a list:

  1. Using an index

    As each position of the array is indexed with a positive number from 0 to the length of the array minus 1, you can access the positions of the array with an incremental index. So, for example:

    i = 0 
    while i < len(arr):
        print(arr[i])
        i = i   1
    

    will access arr[0] in the first iteration, arr[1] in the second iteration and so on, up to arr[len(arr)-1] in the last iteration. Then, when i is further incremented, i = len(arr) and so the condition in the while loop (i < arr[i]) becomes false. So the loop is broken.

  2. Using an iterator

    I won't go in the details of how an iterator works under the surface since it may be too much to absorb for a beginner. However, what matters to you is the following. In Python you can use an iterator to write the condition of a for loop, as your teacher showed you in the example:

    for x in arr: 
        print(x)
    

    An iterator is intuitively an object that iterates over something that has the characteristic of being "iterable". Lists are not the only iterable elements in python, however they are probably the most important to know. Using an iterator on a list allows you to access in order all the elements of the list. The value of the element of the list is stored in the variable x at each iteration. Therefore:

    iter 1: x = arr[0]
    iter 2: x = arr[1]
    ...
    iter len(arr)-1: x = arr[len(arr)-1]
    

    Once all the elements of the list are accessed, the loop terminates.

Note: in python, the function range(n) creates an "iterable" from 0 to n-1, so the for loop

for i in range(len(arr)): 
    print(arr[i])

uses an iterator to create the sequence of values stored in i and then i is in turn used on the array arr to access its elements positionally.

Summing the elements. If you understand what I explained to you, it should be straightforward to write a loop to sum all the elements of a list. You initialize a variable sum=0 before the loop. Then, you add the element accessed as we saw above at each iteration to the variable sum. It will be something like:

sum = 0    
for x in arr:
    sum = sum   x

I will let you write an equivalent code with the other two methods I showed you and do the other points of the assignment by yourself. I am sure that once you'll understand how it works you'll be fine. I hope to have answered your question.

CodePudding user response:

She wants you to loop through the list. Python is really nice makes this easier than other languages. I have an example below that is close to what you need but I do not want to do your homework for you.

listName = [4,8,4,7,84]

for currentListValue in listName:
    #Do your calculating here...
    #Example: tempVar = tempVar   (currentListValue * 2)

as mentioned in the comments w3schools is a good reference for python.

  • Related