Python Script:
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
print(any_lowercase2('Ansar'))
print(any_lowercase2('ansar'))
I was expecting to have an error but instead, I am getting output as True
for both.
CodePudding user response:
This code will always return True
because it's checking to see if the string 'c'
is lowercase, not the variable named c
.
Change the condition to:
if c.islower():
The function will also return after only checking the first letter in the input string. You can fix this by moving the else
case so it only returns after the loop is complete.
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
return 'False'
This will return 'False'
if the input is all uppercase.