Search code examples
pythondictionaryfor-loopnested

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


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"


Solution

  • 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.