Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. stop = False
  2. marks = {}
  3.  
  4. while not stop:
  5. print("1 … Load marks from file")
  6. print("2 … Save marks to file")
  7. print("3 … Add/Update a subject with mark")
  8. print("4 … Show all marks")
  9. print("0 … exit")
  10.  
  11. command = input("Enter your choice:")
  12. if(not command.isnumeric()):
  13. print ("Please enter a number!")
  14. continue
  15.  
  16. command = int(command)
  17.  
  18. if(not (command <= 4 and command >= 0)):
  19. print("Please choose one of the choices given!")
  20. continue
  21.  
  22.  
  23. if command == 0:
  24. print("Exiting...")
  25. stop = True
  26.  
  27. if command == 1:
  28. file_name = input("Enter filename: ")
  29. try:
  30. with open(file_name) as f:
  31. for line in f:
  32. subject, mark = line.split(';')
  33. mark = mark.replace('\n', '')
  34. sub_marks = []
  35.  
  36. for i in range(0, len(mark),1):
  37. sub_marks.append(int(mark[i]))
  38.  
  39. marks[subject] = sub_marks
  40.  
  41. except FileNotFoundError:
  42. print('File not found!')
  43. except IOError:
  44. print('Error reading file!')
  45. else:
  46. print('Reading finished')
  47.  
  48. elif command == 2:
  49. file_name = input("Enter filename: ")
  50. try:
  51. with open(file_name, 'w') as f:
  52. for subject, mark in marks.items():
  53. f.write(subject + ";" + ''.join(map(str, mark)) + '\n')
  54. except IOError:
  55. print('Error writing file!')
  56. else:
  57. print("Saved!")
  58.  
  59. elif command == 3:
  60. subject = input("Subject:")
  61. mark = input("Mark:\t")
  62. try:
  63. if subject in marks:
  64. marks[subject].append(int(mark))
  65. else:
  66. marks[subject] = [int(mark)]
  67. except ValueError:
  68. print("Mark has to be a number!")
  69.  
  70. elif command == 4:
  71. for item, value in marks.items():
  72. print(item + "\t->\t" + str(value))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement