Python Forum
Help With Coding Assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help With Coding Assignment
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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)
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  please Help in my coding assignment thunder 2 603 Nov-12-2023, 09:05 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020