Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can someone explain why isdigit() isn't working with this project and how can i make this happen?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading