As a beginner practice I have to create a function that returns True if ALL the letters in the string are uppercase. I am able to do that but it is returning False if only a string of symbols is inputted (ex. #$%) when it should be returning True.
Here is the code, it will return True if all letters are capital & include symbols but will not return True if only containing symbols.
def is_uppercase(inp):
if inp.isupper() and inp != int:
return True
else:
return False
The exact input that is failing is $%&.
>Solution :
Your problem is, .isupper() will return False for anything that is not an uppercase character, including symbols and digits. You will need to use the inverse (not) of .islower() instead. You should also iterate over the string and check each character individually:
def is_uppercase(inp):
return not any(i.islower() for i in inp)
The (i.islower() for i in inp) part is a generator expression that iterates over the string and returns the .islower() value of the next character every time it’s called.
any() will compute every value of the generator expression until it finds True.
not will inverse the output.
Basically it will return False if it finds a non-uppercase character, and True if it can’t.