Python Forum

Full Version: Need help decoding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

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 understand
Can someone shed a light on this? I'm thoroughly confused. The comments are my own
Especially for assignments, instead of searching for code, the point of them is to solve the problems by yourself, to develop those skills. Where did you get stuck with that?
Hi,

A few days ago, in the section "general coding help", an interesting topic was started
named "a better way of writing code".
There a method is discussed to identify how many vowels are in a random string.
This is similar to what you want to do, but only for the fist letter.
Maybe you can find some inspiration there.
Paul
It isn't recommended that you search for answers on the internet but post your question here.
I would do differently.
I would use split to split the sentence into words, then check the lower() of the first character of each word. Print if in that range, don't print if not.
In fact,

the steps are provided in the assignment:

split the words by building a placeholder variable: word

loop each character in the input string
check if character is a letter
add a letter to word each loop until a non-alpha char is encountered
if character is alpha

add character to word
non-alpha detected (space, punctuation, digit,...) defines the end of a word and goes to else
else

check if word is greater than "g" alphabetically
This is the part I dont understand, ok i get it to test if word is greater than "g" alphabetically, but why must it be "go" and not "g", what's the difference? And the results will be different if i replace "go" with say "all". What i understand is, if word >= "a", this means as long as word is "b" or greater...is that correct?
print word
set word = empty string
Why must we set word to become empty string here?
or else
set word = empty string and build the next word
Hint: use .lower()

Consider how you will print the last word if it doesn't end with a non-alpha character like a space or punctuation?

# [] create words after "G"
# sample quote "Wheresoever you go, go with all your heart" ~ Confucius (551 BC - 479 BC)

-----------------------------------------------------------------
The version you found checks against "go" as you are supposed to get words h-z. This is an error as it will pass the words "guess" and "gyroscope". You should check against "h", and use >=.
You set word to "" in order to reset for the next word. Otherwise all the words would run together.
(Jun-16-2020, 02:47 PM)jefsummers Wrote: [ -> ]The version you found checks against "go" as you are supposed to get words h-z. This is an error as it will pass the words "guess" and "gyroscope". You should check against "h", and use >=.
You set word to "" in order to reset for the next word. Otherwise all the words would run together.

Yes i agree