Jun-15-2020, 05:05 AM
Hi,
This pertains to a question in my assignment where I'm required to: Create a program inputs a phrase (like a famous quotation) and prints all of the words that start with h-z
Sample input:
enter a 1 sentence quote, non-alpha separate words: Wheresoever you go, go with all your heart
Sample output:
WHERESOEVER
YOU
WITH
YOUR
HEART
-----------------------------------
I was stuck and went to find help online. I chanced upon the code below which proved to be correct but I dont' quite understand the logic.
This pertains to a question in my assignment where I'm required to: Create a program inputs a phrase (like a famous quotation) and prints all of the words that start with h-z
Sample input:
enter a 1 sentence quote, non-alpha separate words: Wheresoever you go, go with all your heart
Sample output:
WHERESOEVER
YOU
WITH
YOUR
HEART
-----------------------------------
I was stuck and went to find help online. I chanced upon the code below which proved to be correct but I dont' quite understand the logic.
sentence = "wheresover you go, go with all your heart " word = "" for i in sentence: #the "for" function will iterate every letter if i.isalpha(): #if the iteration of every letter is an alphabet, the alphabet will be added on to the variable "word" as per below, correct? word += i.lower() else: if word > "go": print(word.upper()) word = "" else: word = "" #I don't quite understand the code above. # Why do we test word > "go"? Which will exclude the word "all" in the sentence as well. It won't work if I replace "go" with "all". Since the code test for word > "all", shouldn't it work too? #What has lower case and upper case help in this code? I don't quite understandCan someone shed a light on this? I'm thoroughly confused. The comments are my own