print average percentage of a student from an array
<p>The program is supposed to calculate and print out a given student's average percentage.</p>

<p>Unfortunately I am only able to print out the average percentage of the last student name in the array. 
I want to know where exacltly I am going wrong with my coding.
Thanks. Heres my code below.</p>

<pre><code>def averagepercentage():

    scores = int(name_marks[1]),int(name_marks[2]),int(name_marks[3])
    ap = sum(scores)/3

    return ap

N = int(input('Number of students: ')) # total number of students
marks = int()
arr = []

for i in range(N):
    name_marks = input('name &amp; marks').split() #enter name &amp; three different scores 
    name = str(name_marks[0])
    arr.append(name)
    print(arr)

student_name = str(input('student_name'))
for x in arr:
    if student_name in x:

        print (x)

        print("%.2f" %averagepercentage())
</code></pre>

<p>In your first loop:</p>

<pre><code>for i in range(N):
    name_marks = input('name &amp; marks').split() #enter name &amp; three different scores 
    name = str(name_marks[0])
    arr.append(name)
    print(arr)
</code></pre>

<p>you don't store the marks of the previous students, you replace your variable <code>name_marks</code> by the last marks from your input</p>

<p>the <code>if student_name in x:</code> looks if <code>student_name</code> is included in <code>x</code>, which is not exactly what you want to do, consider doing <code>if student_name == x:</code> instead.</p>

<p>Then:</p>

<pre><code>def averagepercentage():

    scores = int(name_marks[1]),int(name_marks[2]),int(name_marks[3])
    ap = sum(scores)/3

    return ap
</code></pre>

<p>looks at the global variable <code>name_marks</code> to compute the average, but this variables only contains the value of the last student (because of my first remark)</p>

<p>what you can do, to keep most of your code structure is:</p>

<pre><code>for i in range(N):
    name_marks = input('name &amp; marks').split() #enter name &amp; three different scores 
    name = str(name_marks[0])
    arr.append((name,averagepercentage()))
    print(arr)

student_name = str(input('student_name'))

for x in arr:
    if student_name in x:
        print ("student :" + x[0])
        print("average :" + x[1])
</code></pre>

python arrays python-3.x iteration