Help With Coding Assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Help With Coding Assignment (/thread-11455.html) |
Help With Coding Assignment - sarah_mb_sues - Jul-09-2018 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: *********************************************** # 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) RE: Help With Coding Assignment - ichabod801 - Jul-10-2018 Look at the last bit in your instructions, that tells you what you need to do. There are two ways you are not following those instructions. First, you are testing if character is greater than 'g', not if the word is. And note that because of the if clause before that, character is not even a letter. Second, you are not resetting the word variable, so it's glomming everything into one word, and the test against 'g' will always be the same. RE: Help With Coding Assignment - sarah_mb_sues - Jul-10-2018 You're right, that is where the trouble is coming from in my code. I don't know how to fix this still, since I tried adding an "elif" statement to check for characters greater than "g," and I reset the word variable to an empty string, but it still isn't working # 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 elif character > "g": print(word.upper()) word = "" else: pass print(quote) RE: Help With Coding Assignment - ichabod801 - Jul-10-2018 You are still checking character and not word against 'g' (line 11). Also note that your instructions say to reset word in two places. You are only doing it in one. RE: Help With Coding Assignment - sarah_mb_sues - Jul-10-2018 I found out how to arrange the code so that it only printed words starting with characters greater than "g," and it is running smoothly now. Thank you so much for your help! |