Rachel Swamy Rachel Swamy - 6 months ago 9
Python Question

Sorting Data and printing into a file

I have five functions i) lab average ii) program_average iii) midterm_average iv) final and v) weighted_total_score

How do i create a new function that will sort data based on one of the options above and write it into a file of the users choice?

My code so far is rather repetitive. Is there a way I can condense this?

def sorted_data(student_name):

print("This option is for sorting students data and printing in a file")
print("(i) lab average, (ii) program average, (iii) midterm average, (iv) final, (v) weighted total score")
user_sorted_data=(input("Select one of the options (i-v):"))
write_sorted_file=print(input("What file would you like this written into?"))

if (user_sorted_data=='i'):
print("You have selected sorting student data upon lab average")
f=open('write_sorted_file','w')
f.write (str(student_lab_average(student_scores)))
f.close()

if (user_sorted_data=='ii'):
print("You have selected sorting student data upon program average")
f=open('write_sorted_file','w')
f.write (str(student_prog_average(student_scores)))
f.close()

if (user_sorted_data=='iii'):
print("You have selected sorting student data upon midterm average")
f=open('write_sorted_file','w')
f.write (str(mid_average(student_scores)))
f.close()

if (user_sorted_data=='iv'):
print("You have selected sorting student data upon the final grade")
f=open('write_sorted_file','w')
f.write (str(overall_grade(student_scores)))
f.close()

if (user_sorted_data=='v'):
print("You have selected sorting student data upon weighted total score")
f=open('write_sorted_file','w')
f.write (str(weighted_total_score(student_scores)))
f.close()


I am calling the function like so

elif(ch== 'e'):
print(" ")
student_name=input("Type the student's last name:")
print(" ")
scores= get_data_for_student(student_name,mid1,mid2,final,homework,labs,program1,program2,program3,participation)
f=open('write_sorted_file', 'w')
print(" ")
f.write("Here are all the numbers in the text file sorted.") + str(sorted_data(scores))
f.close()

Answer

This is as small as I can condense,

options_functions = {'i':student_lab_average,
       'ii':student_prog_average,
       'iii':overall_grade,
       'iv': overall_grade,
       'v':weighted_total_score
                 }
options_strings = {'i':'lab average',
       'ii':'program average',
       'iii':'midterm average',
       'iv': 'final grade',
       'v':'weighted total score'
                 }

def sorted_data(student_name):

    print("This option is for sorting students data and printing in a file")
    print("(i) lab average, (ii) program average, (iii) midterm average, (iv) final, (v) weighted total score")

    user_sorted_data=input("Select one of the options (i-v):")

    write_sorted_file=input("What file would you like this written into?")

    print("You have selected sorting student data upon "+options_strings[user_sorted_data])
    f=open('write_sorted_file','w')
    f.write(str(options_functions[user_sorted_data](student_scores)))
    f.close()
Comments