I want to know if person got full point like 5/5 in all exams, i will append key to a list.
# dictionary could be larger
dicti = {'John': ['5/5', '50/50', '10/10', '10/10']}
liste = []
def f():
for key, value in dicti.items():
count = 0
for i in value:
if i.isdigit(): # kkk
count += 1
if len(value) == count:
liste.append(key)
print(liste)
f()
# I realized in # kkk part doesn't see 5/5 as a digit.
# How can i make this happen?
>Solution :
'5/5' is a string, and isdigit() method will only return True if all of characters are digits. It is not because of '/'. On the other hand, Python doesn’t evaluate the content of the string. It is an object itself ! (I do not recommend to use eval to evaluate that string if that is what you intend to do)
Instead you can check that yourself by writing a small helper function which checks too see if he/she gets a complete score or not:
dicti = {
'John': ['5/5', '50/50', '10/10', '10/10'],
'test_person': ['5/5', '49/50']
}
def is_full(x):
left, right = x.split('/')
return left == right
lst = []
for k, v in dicti.items():
if all(is_full(grade) for grade in v):
lst.append(k)
print(lst)
output:
['John']