python - Sorting Data and printing into a file -


i have 5 functions i) lab average ii) program_average iii) midterm_average iv) final , v) weighted_total_score

how create new function sort data based on 1 of options above , write file of users choice?

my code far rather repetitive. there way can condense this?

def sorted_data(student_name):      print("this option sorting students data , printing in file")     print("(i) lab average, (ii) program average, (iii) midterm average, (iv) final, (v) weighted total score")     user_sorted_data=(input("select 1 of options (i-v):"))     write_sorted_file=print(input("what file 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 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 calling function so

elif(ch== 'e'):     print(" ")     student_name=input("type 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 numbers in text file sorted.") +    str(sorted_data(scores))     f.close() 

this small 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_scores):      print("this option sorting students data , printing in file")     print("(i) lab average, (ii) program average, (iii) midterm average, (iv) final, (v) weighted total score")      user_sorted_data=input("select 1 of options (i-v):")      write_sorted_file=input("what file 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() 

edit:

the variable names not same.

you using student_name local variable name when defining function passing scores. change 'student_scores'.

student_scores variable doesn't exist either globally or locally raising error student_scores not defined

and there error @ line

f.write("here numbers in text file sorted.") + str(sorted_data(scores))

function sorted_data not returning anything. returns none

you can't add function , string

put f.write("here numbers in text file sorted.") inside sorted_data writing file inside sorted_data or open append, if open write replace previous data


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -