Jul-09-2018, 10:23 PM
(This post was last modified: Jul-09-2018, 10:23 PM by sarah_mb_sues.)
Hello Everyone,
I am having trouble with setting up a loop in my edX coding assignment -- when I run what I have, it produces very bizarre results that are far from what I want.
Can someone explain what is wrong with my code, or how to go about setting up the loop properly? I know there must be some problems with what I have, but I don't know how to address them. Thank you!
Here are the guidelines for the assignment:
**********************************************
Program: Words after "G"/"g"
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
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
print word
set word = empty string
or else
set word = empty string and build the next word
************************************************
Here is my code so far:
***********************************************
I am having trouble with setting up a loop in my edX coding assignment -- when I run what I have, it produces very bizarre results that are far from what I want.
Can someone explain what is wrong with my code, or how to go about setting up the loop properly? I know there must be some problems with what I have, but I don't know how to address them. Thank you!
Here are the guidelines for the assignment:
**********************************************
Program: Words after "G"/"g"
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
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
print word
set word = empty string
or else
set word = empty string and build the next word
************************************************
Here is my code so far:
***********************************************
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# create words after "G" # quote = "Wheresoever you go, go with all your heart" quote = input ( "Enter a one sentence quote in non-alpha separate words: " ) word = "" for character in quote: if character.isalpha(): word + = character.upper() elif character > = "h" : print (word.capitalize()) else : print (word) print (quote) |