How do I return "Yes" if all words in the sentence are lowercase?

Viewed 222

I can currently have my code return "Yes" for each character, but I'm not sure how to make the code return just ONE Yes at the end of the code, if every word in the sentence was lowercase. Here's what I have

sentence = "hello thEre is this aLL lowercase"
sent = sentence.split(" ")
lower = False
for wd in sent:
    for ch in wd:
        if ch.islower():
            print("Yes")
            lower = True
        if not ch.islower():
            print("No")
            lower = False

I know I cannot have print("Yes") in the loop because it will print everytime, but I don't know how to do it any other way. Thanks for any help.

8 Answers

Here is one way to approach the solution.

I’m intentionally not providing any explanation, because it will help your learning to research the concepts on your own. Look into:

  • List comprehension
  • The all() function
  • Boolean tests

Sample code:

s = ‘lower case statement’

result = all([i.islower() for i in s.split()])

print('Yes' if result else 'No')
>>> ‘Yes’

I recommend taking each part of the short code apart to discover how it’s working.

Simplified:

Personally, I don’t mind sharing this in an educational setting, because part of learning to write Python (and code in general) is learning to write it efficiently. That said, here’s a simplified solution:

print('Yes' if s.islower() else 'No')
>>> ‘Yes’

If you only care about it being all lowercase then the first time you find a capital letter you should break out of the loop. Then only print yes if lower is true outside the loop.

I'm assuming this is an exercise, so I won't give any code.

You need to have a variable "any non-lowercase word found" that is set to false before the loop and set to true if a word is found that is not lowercase. If the variable is still false after the loop you can print "yes", otherwise not.

Maybe if you have written the code to implement this you will find that it can be optimized.

You can use isLower(), just remember to remove spaces

def checkLower(string):
    string = string.replace(" ","")
    print(string)
    for i in string:
        if i.islower() == False:
            return "no"
    return "yes"

You can simply do this way using .islower() because The islower() methods return True if all characters in the string are lowercase, Otherwise, It returns False.

sentence = "hello thEre is this aLL lowercase"
if(sentence.isLower()):
    print('Yes')
else:
    print('No')
sentence = "hello there this is all lowercase"
sent = sentence.split(" ")
lower = True
for wd in sent:
    for ch in wd:
        if not ch.islower():
            lower = False
if lower:
   print("Yes")
if not lower:
   print("No")

I don't have much idea about python and how it works but here is the logic i added to the code provided.

Right now you are printing "Yes" every time you find a lowercase word.

def isAllLower(sentence):
    sent = sentence.split(" ")
    lower = False
    for wd in sent:
        for ch in wd:
            if ch.islower():
                lower = True
            if not ch.islower():
                lower = False
    if lower == True:
        print("Yes")
    elif lower == False:
        print("No")

isAllLower("hello thEre is this aLL lowercase")

The simplest by far way is to compare the lowered by default sentence using the lower() function, with the initial function!

def SentenceisLower(sentence):
    sentencelower = sentence.lower()
    if (sentence == sentencelower):
        print("Yes!")

Where there is no reply in any other outcome!

Related