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: 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…

How can I solve this issue?

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement