How can i calculate the sum of numbers in a nested dict. in python

Question:

How can i calculate the sum or total points of numbers of this nested dict. in python

i tried alot of things and it didn’t work all time get the last value in the loop not every grade or value.
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………

# Input


students = {
  "Ahmed": {
    "Math": "A",
    "Science": "D",
    "Draw": "B",
    "Sports": "C",
    "Thinking": "A"
  },
  "Sayed": {
    "Math": "B",
    "Science": "B",
    "Draw": "B",
    "Sports": "D",
    "Thinking": "A"
  },
  
}

grades = {
  "A": 100,
  "B": 80,
  "C": 40,
  "D": 20
}



for main_key, main_value in students.items():
  print("------------------------------")
  print(f"-- Student Name => {main_key}")
  print("------------------------------")

  for subject, rank in main_value.items():
    print(f"- {subject} => {grades[rank]} Points")
  else:  
    # Can't caculate the total points
    print(f"Total Points For {main_key} Is ###")









# Needed Output
"------------------------------"
"-- Student Name => Ahmed"
"------------------------------"
"- Math => 100 Points"
"- Science => 20 Points"
"- Draw => 80 Points"
"- Sports => 40 Points"
"- Thinking => 100 Points"
"Total Points For Ahmed Is 340"
"------------------------------"
"-- Student Name => Sayed"
"------------------------------"
"- Math => 80 Points"
"- Science => 80 Points"
"- Draw => 80 Points"
"- Sports => 20 Points"
"- Thinking => 100 Points"
"Total Points For Sayed Is 360"

Asked By: Mahmoud Dwidar

||

Answers:

You can do it like this:

students = {
  "Ahmed": {
    "Math": "A",
    "Science": "D",
    "Draw": "B",
    "Sports": "C",
    "Thinking": "A"
  },
  "Sayed": {
    "Math": "B",
    "Science": "B",
    "Draw": "B",
    "Sports": "D",
    "Thinking": "A"
  },
  
}

grades = {
  "A": 100,
  "B": 80,
  "C": 40,
  "D": 20
}



for main_key, main_value in students.items():
  print("------------------------------")
  print(f"-- Student Name => {main_key}")
  print("------------------------------")

  for subject, rank in main_value.items():
    print(f"- {subject} => {grades[rank]} Points")
  else:  
    print(f"Total Points For {main_key} Is {sum(grades[grade] for grade in main_value.values() if grade)}")

The output of the code will be, which matches your desired output:

------------------------------
-- Student Name => Ahmed
------------------------------
- Math => 100 Points
- Science => 20 Points
- Draw => 80 Points
- Sports => 40 Points
- Thinking => 100 Points
Total Points For Ahmed Is 340
------------------------------
-- Student Name => Sayed
------------------------------
- Math => 80 Points
- Science => 80 Points
- Draw => 80 Points
- Sports => 20 Points
- Thinking => 100 Points
Total Points For Sayed Is 360

Hope it helps.

Answered By: galoget

I think you were 90% there. You just needed to clean up your indentation, and fix some minor typos, but the rest was essentially fine.

students = {
    "Ahmed": {
        "Math": "A",
        "Science": "D",
        "Draw": "B",
        "Sports": "C",
        "Thinking": "A"
    },
    "Sayed": {
        "Math": "B",
        "Science": "B",
        "Draw": "B",
        "Sports": "D",
        "Thinking": "A"
    }
  }

grades = {
    "A": 100,
    "B": 80,
    "C": 40,
    "D": 20
}



for main_key, main_value in students.items():
    print("------------------------------")
    print(f"-- Student Name => {main_key}")
    print("------------------------------")
    
    total = 0

    for subject, rank in main_value.items():
        print(f"- {subject} => {grades[rank]} Points")
        total += grades[rank]
    
    # Can't caculate the total points
    print(f"Total Points For {main_key} Is {total}")

OUTPUT:

------------------------------
-- Student Name => Ahmed
------------------------------
- Math => 100 Points
- Science => 20 Points
- Draw => 80 Points
- Sports => 40 Points
- Thinking => 100 Points
Total Points For Ahmed Is 340
------------------------------
-- Student Name => Sayed
------------------------------
- Math => 80 Points
- Science => 80 Points
- Draw => 80 Points
- Sports => 20 Points
- Thinking => 100 Points
Total Points For Sayed Is 360
Answered By: ScottC

To help you clarify your issue, it doesn’t seem to be about sum of values in a nested dictionary instead calculating the sum of mapped values for keys in an iterable. The same solution can be applied to both but adding examples to explain the terminology.

With your code as a base,

students = {
  "Ahmed": {
    "Math": "A",
    "Science": "D",
    "Draw": "B",
    "Sports": "C",
    "Thinking": "A",
  },
  "Sayed": {
    "Math": "B",
    "Science": "B",
    "Draw": "B",
    "Sports": "D",
    "Thinking": "A",
  },
}

grades = {
  "A": 100,
  "B": 80,
  "C": 40,
  "D": 20,
}

Now we will hold and maintain the intermediate sum of grade values for each student (each iteration)

for student, report in students.items():
  print("------------------------------"
  print(f"-- Student Name => {student}")
  print("------------------------------")

  student_total = 0  # Set the initial total for each student as 0

  for subject, grade in report.items():
    print(f"- {subject} => {grades[rank]} Points")
    student_total += grades[rank]  # Add the value of the current grade to student's total
  print(f"Total Points for {student} = {student_total}")

FYI:
A nested dictionary of values would be like:

students = {
  "Ahmed": {
    "Math": 100,
    "Science": 20,
    "Draw": 80,
    "Sports": 40,
    "Thinking": 100,
  },
  "Sayed": {
    "Math": 80,
    "Science": 80,
    "Draw": 80,
    "Sports": 20,
    "Thinking": 100,
  },
}

You can use the same approach to calculate the sum here as well

Answered By: DarkElf73
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.