vendredi 6 août 2021

How to get the each value from the dictionary and the sum of all value from the dictionary at the same time in python?

I have a task to get the each value and the sum of all value at the same time .

Already tried the below code but couldn't get the result because I need the both sum of all values and each value at the same time and it isn't happening in the loop . That's why , I am searching this solution without using loop.

name = input('Enter Your name :- ')
n = 0
d = {}

while n<3:
    subject_name = input('Enter subject\'s name :- ')
    subject_mark = int(input('Enter Your subject\'s mark :- '))
    d.update({subject_name:subject_mark})
    n+=1

s = 0


for i in d.values():
    if (i<33 and sum(d.values())<100):
        print(f'{name.capitalize()} , You failed in the exam because you got less than 33 in one of your subjects')
        break
    else:
        print(f'{name.capitalize()} , Congratulations!!!You passed the exam')
    s+=i
    

if(s<100):
    print(f'{name} , You failed in the exam because total number is less than 100')
else:
    print(f'{name} , Congratulations!!!You passed the exam')

Aucun commentaire:

Enregistrer un commentaire