Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- stop = False
- marks = {}
- while not stop:
- print("1 … Load marks from file")
- print("2 … Save marks to file")
- print("3 … Add/Update a subject with mark")
- print("4 … Show all marks")
- print("0 … exit")
- command = input("Enter your choice:")
- if(not command.isnumeric()):
- print ("Please enter a number!")
- continue
- command = int(command)
- if(not (command <= 4 and command >= 0)):
- print("Please choose one of the choices given!")
- continue
- if command == 0:
- print("Exiting...")
- stop = True
- if command == 1:
- file_name = input("Enter filename: ")
- try:
- with open(file_name) as f:
- for line in f:
- subject, mark = line.split(';')
- mark = mark.replace('\n', '')
- sub_marks = []
- for i in range(0, len(mark),1):
- sub_marks.append(int(mark[i]))
- marks[subject] = sub_marks
- except FileNotFoundError:
- print('File not found!')
- except IOError:
- print('Error reading file!')
- else:
- print('Reading finished')
- elif command == 2:
- file_name = input("Enter filename: ")
- try:
- with open(file_name, 'w') as f:
- for subject, mark in marks.items():
- f.write(subject + ";" + ''.join(map(str, mark)) + '\n')
- except IOError:
- print('Error writing file!')
- else:
- print("Saved!")
- elif command == 3:
- subject = input("Subject:")
- mark = input("Mark:\t")
- try:
- if subject in marks:
- marks[subject].append(int(mark))
- else:
- marks[subject] = [int(mark)]
- except ValueError:
- print("Mark has to be a number!")
- elif command == 4:
- for item, value in marks.items():
- print(item + "\t->\t" + str(value))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement