2

I am new to Python programming and I created a method to accept a new student name and new grade into an existing dictionary through user input from cmd, but I am finding it difficult to sort. How do I sort that out?

I have been able to code the other methods properly. Only the above method is giving errors at run-time:

StudDict={'James':[70,75,95,],

      'Adams':[75,90,73],

      'Benjamin':[80,79,85]}

#The below is where im having problems.

def AddStudent(): #This method adds new student and grade to the existing Student dictionarydictionary

    StudToAdd= input('ENTER STUDENT NAME TO BE ADDED:  ')

    GradesToAdd=input("ENTER NEW STUDENT'S GRADE:  ")

    StudDict[StudToAdd].append(GradesToAdd)

    print(StudDict)

I expect to be able to add new students(keys),and grades(values) to the existing student dictionary which I have already created and find the new added details in the dictionary.

Nick
  • 4,820
  • 18
  • 31
  • 47
Abayomi Olowu
  • 199
  • 1
  • 10
  • 1
    look at this could be helpfull https://stackoverflow.com/questions/9001509/how-can-i-sort-a-dictionary-by-key – Bob White Jul 24 '19 at 22:24
  • please try to set data to dictionary by this way StudDict[StudToAdd] = GradesToAdd – Bob White Jul 24 '19 at 22:26
  • it's best to follow PEP 8 naming conventions, for functions+variables use lowercase and separated by underscores if necessary - https://www.python.org/dev/peps/pep-0008/#function-and-variable-names – Andrew Allen Jul 24 '19 at 22:31
  • Thanks everyone,all your suggestions worked,i appreciate this community – Abayomi Olowu Jul 25 '19 at 08:19

2 Answers2

2

The reason that your code didn't work is because your new student dictionary isn't defined as a list; therefore, you can't append.

There are couple ways of doing this but keeping it the same logic as you have now, you'll need to add a few extra steps.

>>> students = {'James': [70, 75, 95], 'Adams': [75, 90, 73], 'Benjamin': [80, 79, 85]}
>>> def add_student ():
...     new_student = input ('Enter student name to be added:' )
...     grades = input ('Enter new student\'s grade: ')
...     grades = grades.split (',')  # Split grades by comma
...     grades = [int (grade.strip (' ')) for grade in grades]  # Strip space between each grade & convert to integers
...     students [new_student] = grades  # Assign new student with grades
... 
>>> add_student()
Enter student name to be added:>? John
Enter new student's grade: >? 82, 79, 77
>>> students
{'Benjamin': [80, 79, 85], 'Adams': [75, 90, 73], 'James': [70, 75, 95], 'John': [82, 79, 77]}

Updating answer based on comments
To remove the students score, you'll need ensure that the name as well as the score exists in database.

>>> def remove_scores():
...     name = input('Enter student name whose scores would be removed: ')
...     score = int (input('Enter score to be removed: '))
...     # Checking both student name and score exists in database
...     if name in students and score in students [name]:
...         score_index = students [name].index (score)  # Obtain the index location of the score
...         del students [name][score_index]
...         print (students)
...     else:
...         print('Student %s and/or %s score is not in our database.' % (name, score))
... 

First, let's add one extra score '79' to Adams to be deleted.

>>> students ['Adams'].append (79)
>>> students
{'Benjamin': [80, 79, 85], 'Adams': [75, 90, 73, 79], 'James': [70, 75, 97]}

Next, test the name and score condition.

>>> remove_scores()
Enter student name whose scores would be removed: >? Larry
Enter score to be removed: >? 59
Student Larry and/or 59 score is not in our database.
>>> remove_scores()
Enter student name whose scores would be removed: >? Adams
Enter score to be removed: >? 77
Student Adams and/or 77 score is not in our database.

Once we're happy with the result, let's delete and see the results.

>>> remove_scores()
Enter student name whose scores would be removed: >? Adams
Enter score to be removed: >? 79
{'Benjamin': [80, 79, 85], 'Adams': [75, 90, 73], 'James': [70, 75, 97]}
dreamzboy
  • 795
  • 15
  • 31
  • ENTER LOGIN: Admin1 ENTER PASSWORD: Pass123@ welcome ****Admin1**** ****************WELCOME TO STUDENT GRADING SYSTEM**************** [.] - PRINT STUDENT DICTIONARY [0] - ADD NEW STUDENT(S) [1] - ENTER GRADES [2] - REMOVE STUDENT [3] - STUDENT AVERAGE GRADES [4] - EXIT PROGRAM ~~~~~~WHAT WOULD YOU LIKE TO DO(ENTER A NUMBER)~~~~~~: 0 ENTER STUDENT NAME TO BE ADDED: Dreamzboy ENTER NEW STUDENT'S GRADE: 99,99,99 {'James': [70, 75, 95], 'Adams': [75, 90, 73], 'Benjamin': [80, 79, 85], 'Dreamzboy': [99, 99, 99]} – Abayomi Olowu Jul 25 '19 at 08:12
  • Still on the same program,im trying to code a method that can remove scores one after the other.can you help with that too.Here is what i coded. def remove_scores(): StudNaMe=input("ENTER STUDENT'S NAME WHOSE SCORE(S) WOULD BE REMOVED: ") Score_remove=print("ENTER SCORE TO BE REMOVED: ") if StudNaMe in StuDict: StuDict[StudNaMe].remove(Score_remove) print(StuDict) else: print(StudNaMe,"IS NOT IN OUR DATABASE ") – Abayomi Olowu Jul 27 '19 at 19:32
1

Try using the defaultdict, as it seems perfect for what you are trying to do:

from collections import defaultdict

StudDict = defaultdict(list)
# Optionally, provide existing data to the object:
# StudDict = defaultdict(list, {existing_dict_with_data})

def add_student():
    ...
    StudDict[StudToAdd].append(GradesToAdd)

See documentation for more details here:

https://docs.python.org/3/library/collections.html#collections.defaultdict

Sazzy
  • 1,924
  • 3
  • 19
  • 27