python - Writing a file after a string -


i want read , write grades(numbers) after subject tag(string). have can't figure out how find tag , write after without replacing numbers after it.

so, example, valid input be:

mat54324524342211 

and have tried far:

save = open("grades.txt", "w")  def add(x, y):     z = x / y * 100     return z  def calc_grade(perc):     if perc < 50:         return "1"     if perc <  60:         return "2"     if perc < 75:         return "3"     if perc < 90:         return "4"     if perc >= 90:         return "5"  def calc_command():     num1 = input("input points: ")     num2 = input("input maximum points: ")     num3 = add(float(num1), float(num2))     grade = calc_grade(num3)     print("this result:", str(num3) + "%")     print("your grade:", grade)     save.write(grade)   while true:     command = input("input command: ")     if command == "calc":         calc_command()     if command == "exit":         break 

any ideas? hey sorry old version of code. new version after printing grade here save.write(grade). program not finished. final idea bunch of subject tags in txt file , user pick subject grade.

please try not reimplement things come standard library.

some implementation of script using json this:

import os import json   def read_grades(filename):     if os.path.exists(filename):         open(filename, 'r') f:             try:                 return json.loads(f.read())             except valueerror ex:                 print('could not read', ex)     return {}   def write_grades(filename, grades):     open(filename, 'w') f:         try:             f.write(                 json.dumps(grades, indent=2, sort_keys=true)             )         except typeerror ex:             print('could not write', ex)   def question(text, cast=str):     try:         inp = input(text)         return cast(inp)     except exception ex:         print('input error', ex)         return question(text, cast=cast)   def calculator(grades):     def add(x, y):         assert y != 0, 'please don\'t that'         z = x / y         return z * 100      def perc(res):         n, p in enumerate([50, 60, 75, 90], start=1):             if res < p:                 return n         return 5      subject = question('enter subject: ').strip()     points = question('your points: ', cast=float)     max_points = question('maximum points: ', cast=float)      result = add(points, max_points)     grade = perc(result)      print('> {subject} result: {result:.2f}% grade: {grade}'.format(         subject=subject.capitalize(),         result=result, grade=grade     ))      grades.setdefault(subject.lower(), list())     grades[subject.lower()].append(grade)      return grades   def show(grades):     print('>> grades')     subject in sorted(grades.keys()):         print('> {subject}: {grades}'.format(             subject=subject.capitalize(),             grades='; '.join(str(g) g in grades[subject])         ))   def main():     filename = 'grades.json'     grades = read_grades(filename)      while true:          grades = calculator(grades)         show(grades)          if not question('continue? (hit enter quit) '):             break      write_grades(filename, grades)  if __name__ == '__main__':     main() 

the grades.json contains dictionary subjects keys, , list of grades values:

{   "art": [     3   ],   "math": [     4,     5   ] } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -