Skip to content
Advertisement

Letters always detected as uppercase even when they’re lowercase

I am working on a very simple project that detects whether a letter is uppercase or lowercase and replaces it with a random letter that’s uppercase or lowercase like it while skipping spaces, so, for example, “Oh Boy” should output something like “Wx Dai”

but here’s the problem, the code is misbehaving. When I call the check(letter) function it always detects the letter as uppercase so it always returns true, so, “Oh Boy” would output something like “WX DAI” when it shouldn’t…

any way I can solve that issue? thanks.

import random


def check(letter):
    if letter.isupper:
        return True
    if letter.islower:
        return False


while 3 > 2:
    command = list(input('message? '))
    new_list = []
    for letter in command:
        if letter == ' ':
            new_list.append(letter)
        if letter != ' ':
            if check(letter):
                randomized_upper = random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
                new_list.append(randomized_upper)
            if not check(letter):
                randomized_lower = random.choice('abcdefghijklmnopqrstuvwxyz')
                new_list.append(randomized_lower)
    output = ''.join(new_list)
    print(output)

Advertisement

Answer

It should be letter.isupper() to invoke the function. By leaving off the parentheses it is evaluating to true because letter.isupper is defined (as a function) and functions always evaluate to boolean true in python’s dynamic typing. In fact, everything evaluates to true except 0, None, {}, [], False, (), and “”

https://docs.python.org/3/library/stdtypes.html#truth-value-testing

Advertisement