BeggineratPython
BeggineratPython

Reputation: 5

I need some code to allow me to find the highest score in my .txt doc

Also with my average it some times prints the name too many times. I have this code but am struggling with showing the scores lowest to highest in my text file it looks like this:
name,1
name,4
name,7
name,9
name,10

This is my code:

import sys

Viewclassnum=  input ('Which class do you want to view 1,2 or 3?')
print('please input a, b or c')
print('a)Alphabeically')
print('b)average')
print('c)highest to lowest')
Viewclasssort= input ('how would you like to view it:')



                                                                                                    #setting variable fd to open the file set to File_name (from when the program asked the user for  

if Viewclassnum=='1' and Viewclasssort=='a':
    with open('class1.txt', 'r') as r:
        for line in sorted(r):
             print(line, end='')
    Again=input('Do you want to view another class yes or no?')
    if Again=='yes':
        Viewclassnum=  input ('Which class do you want to view 1,2 or 3?')
        print('please input a, b or c')
        print('a)Alphabeically')
        print('b)average')
        print('c)highest to lowest')
        Viewclasssort= input ('how would you like to view it:')
    if Again=='no':
        print('Bye')


if Viewclassnum=='1' and Viewclasssort=='b':
  fd = open('class1.txt')
  lines = [line.strip() for line in fd]
  f = {}
  for line in lines:
    split = [i for i in line.split(',')]
    key = split[0]
    f[key] = [int(n) for n in split[1:]]
    avg_mark = lambda name:sum(f[name])/len(f[name])
    for i in sorted(f.keys(),key=avg_mark,reverse=True):
        print (i,avg_mark(i),"\n")
  Again=input('Do you want to view another class yes or no?')
  if Again=='yes':
        Viewclassnum=  input ('Which class do you want to view 1,2 or 3?')
        print('please input a, b or c')
        print('a)Alphabeically')
        print('b)average')
        print('c)highest to lowest')
        Viewclasssort= input ('how would you like to view it:')
  if Again=='no':
        print('Bye')

The output is:

Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:a
name,3
name,8
name,9
2name,4
2name,7
2name,8
Do you want to view another class yes or no?yes
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:b
name 3.0 

name 8.0 

name 9.0 

2name 9.0 

2name 4.0 

name 9.0 (repeated don't know why)

name 7.0 

I would also like to group all of the names together so that it looks like this:

name,3,5,6
2name,4,5,6

and only the three latest scores are shown. For the highest to lowest score i would expect this outcome: name,10 2name,8 5name,4 3name,5

Thanks

Upvotes: 0

Views: 80

Answers (1)

Brobin
Brobin

Reputation: 3326

Here is how I think I would handle this. Becuase dictionaries are not ordered, they cna't be sorted, so I made a list of Student objects which is sortable.

I also reduced a lot of your input code that was repeated 3 times

import sys

class Student:
    def __init__(self, name, marks):
        self.name = name
       self.marks = marks
    def avg_mark(self):
        return sum(self.marks)/len(self.marks)
    def __str__(self):
        return '{0}: {1}, {2}'.format(self.name, self.marks, self.avg_mark())

def load_grades(filename):
    fd = open(filename, 'r')
    f = {}
    for line in fd:
        split = line.split(',')
        key = split[0]
        marks = [int(n) for n in split[1:]]
        try:
           f[key] += marks
        except KeyError:
            f[key] = marks
    students = []
    for key, values in f.items():
        students.append(Student(key, values[-3:]))
    return students

def sort_by_name(students):
    def get_name(student):
    return student.name
    students = sorted(students, key=get_name)
    return students

def sort_by_avg(students):
    def get_avg(student):
        return student.avg_mark()
    students = sorted(students, key=get_avg)
    return students


again = True

while again:
    class_num =  input ('Which class do you want to view 1,2 or 3?')
    print('please input a, b or c')
    print('a)Alphabeically')
    print('b)average')
    print('c)highest to lowest')
    sort = input ('how would you like to view it:')

    if class_num =="1" and sort =="a":
        students = load_grades('class1.txt')
        students = sort_by_name(students)
    elif class_num =="1" and sort =="b":
        students = load_grades('class1.txt')
        students = sort_by_avg(students)

    for student in students:
        print(student)


    Again=input('Do you want to view another class yes or no?')
    again = (Again=='yes')

print('bye')

Example output:

Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:a
2name: [4, 7, 8, 69], 22.0
name: [3, 8, 9], 6.666666666666667
Do you want to view another class yes or no?yes 
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:b
name: [3, 8, 9], 6.666666666666667
2name: [4, 7, 8, 69], 22.0
Do you want to view another class yes or no?no
bye

Upvotes: 0

Related Questions