Encoding function

Hi!
I have just created a short function to check whether characters are in uppercase or not.
When submitting a string that consists of only upper or lower cases, function works fine. But with mixed - upper and lower characters it does not . It does not differ much from Dataquest proposed solution, thus I find it quite difficult to troubleshoot. Would be grateful for any hints!

My Code:

def check_uppercase(string):
    for row in string: 
        if  (65 <= ord(row) and 90 >= ord(row)):
            return True
        else:
            return False

Dataquest solution:

def check_uppercase (pstring):
    for letter in pstring:
        if not (ord(letter) >= 65 and ord(letter)<=90):
            return False
    return True

Take a pen and paper, a small sample string with those mixed characters, and write out what happens at each step of your for loop.

Based on that, think about what might be the issue in your logic - does it break somewhere you didn’t expect, where does it break, why do you think it breaks, what could you do to fix it etc.?

If you get stuck and have more questions, share what you wrote on that paper here and we can work from there.

1 Like

Both loops classify variables row/letter in 65<= and 90>= interval. I did analyze both functions step by step before posting to this forum (and this is basically why I posted the question).

In my code variable in string gets evaluated whether it classifies in 65 <= ord(row) and 90 >= ord(row).
If yes, code returns True, else False.

In Dataquest solution, if variable is not classfied in 65 <= ord(row) and 90 >= ord(row), code returns false. If it does classify in interval 65 <= ord(row) and 90 >= ord(row), code returns true, so we have a double negation here.

In your code, the entire string is not being evaluated. Only the first character of your string is being evaluated.

For the first iteration of your loop, your first character is checked. If it is a capital letter you return True. If it is not a capital letter you return False.

The loop ends, because the return statement exits the function. You don’t move on to the next character in your string as a result.

Compare the two code snippets but focus on the return statement as well and that should help clarify it further.

1 Like

Thank you for the explanation :slight_smile: