Support Forums

Full Version: I need help with my code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can't get my code to work, could someone please help?


HERE IS THE CODE:
#This program should take in the pupil's names and marks from an external text document.
#It should then find the name of the pupil with the top mark amd display their name and mark and save the mark to file


def get_info():
    pupil_firstName = []
    pupil_surname = []
    pupil_marks = []

    file = open("pupils.txt","r")

    for line in file:
        item=line.split()
        if len(item) >1:
            pupil_firstName.append(item[0])
            pupil_surname.append(item[1])
            pupil_marks.append(int(item[2]))

    return pupil_firstName, pupil_surname, pupil_marks

position = 0
def findMax(pupil_marks):
    last = len(pupil_marks)
    max = pupil_marks[0]
    for i in range(1, last):
        if pupil_marks[i] > max:
            max = pupil_marks[i]
            position = i
    return max, position

def displayResults(pupil_firstName, pupil_surname, pupil_marks, position):
    max, position = findMax(pupil_marks)
    print("The pupil who got the highest mark is",pupil_firstName, pupil_surname[position])
    print("their mark was",max)
             

#Main Program
pupil_firstName, pupil_surname, pupil_marks = get_info()
pupil_marks = findMax(pupil_marks)
displayResults(max, pupil_firstName, pupil_surname[postition])

HERE IS WHAT IS IN THE TEXT FILE:

Jane Morrison 45
Alison Thomas 23
Niam Taylor 83
John Clark 123
Alan Molloy 7
Kevin Anderson 120
Raymond Sinclair 99
Brenda Andrews 72
Wendy Elliott 145
Joyce Douglas 173
Lesley Gray 2
Lynn Simpson 188
Sharon Greenwood 120
Dave Gibson 142
Alastair Greig 94
Bobby Douglas 62
Elaine McDonald 104
Gill Weir 19
Gina Tracey 78
Richard Primrose 101
Peter Vivers 173
Linda Coe 126
Waheeda Mohammed 164
Donna Adams 142
Heather Molloy 130
Bill Smith 108
Kirsty Jones 95
Siobhan Easter 62
Lisa Riley 35
John Mackay 100
Lee McPake 93
Rebecca Richoux 72
Grant Ewing 95
Isobel Hogg 105
Janice Knox 62
Anne Kerr 87
Louise Lindsay 88
Stuart Costley 45
Patricia Niven 83
Nina Hogg 44
Margaret Brown 120
Peter Holmes 94
John Sinclair 183
Angela McCourt 154
Enock Wylie 125
KenSun Chung 66
Daniel Ross 109
Sandie Shaw 122
Tracey Ullman 34
Quroum Pasha 126
Hai fr.my fr know very well about python script.you have help means massage me