Ravi
Ravi

Reputation: 69

Not Sure How to Account for The Last Word in a String

I'm writing a program that takes a famous quote or phrase as input and prints out all the words that start with letter h through z.

Below are the guidelines I've been given to write this program:

1) split the words by building a placeholder variable: word 2) loop each character in the input string 3) check if character is a letter 4) add a letter to word each loop until a non-alpha char is encountered

if character is alpha 1) add character to word 2) 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 a) print word b) set word = empty string or else 1) 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?

I have written a function, created a variable that takes the value of whatever the user inputs for the phrases and declared an empty string variable called word. I then created a for loop that iterates through all the characters in the phrase. I then have an if conditional that uses the isalpha() method to assess whether a character is alphabetical, and if true, it will add the character to the previously created word variable.

After that if, I have an else conditional that contains a nested if and else. The nested if will look at the word variable with the lower() method applied and see if it is greater than or equal to 'h'. If it is, it will print the word out and utilize the upper() method. After the word is printed, it will then reset the word variable to an empty string.

If that nested if fails, it goes to the nested else, which will set the word variable back to an empty string.

def test_words_after_g():
    quote = input("enter a 1 sentence quote, non-alpha separate words:")
    word = ''

    for character in quote:
        if character.isalpha() == True:
            word += character
        else:
            if word.lower() >= 'h':
                print(word.upper())
                word = ''
            else:
                word = ''

test_words_after_g()

I haven't figured out how to answer the question in the problem - "Consider how you will print the last word if it doesn't end with a non-alpha character like a space or punctuation?" Below is a sample input and output.

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

And here is my actual input and output

Actual input:

Wheresoever you go, go with all your heart

Actual output:

WHERESOEVER
YOU
WITH
YOUR

I am missing heart in the output, and I am stumped figuring out how to modify my code to include it in the output. Any guidance would be appreciated.

Upvotes: 0

Views: 599

Answers (1)

Stef
Stef

Reputation: 30639

When your for loop has finished, word still contains the last word, so just append the test after the loop:

def test_words_after_g():
    quote = input("enter a 1 sentence quote, non-alpha separate words:")
    word = ''

    for character in quote:
        if character.isalpha() == True:
            word += character
        else:
            if word.lower() >= 'h':
                print(word.upper())
                word = ''
            else:
                word = ''

    if word.lower() >= 'h':
        print(word.upper())

Upvotes: 1

Related Questions