Python Forum
Am I wrong? Or is the question setup wrong?
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Am I wrong? Or is the question setup wrong?
#1
Can someone tell me what, if anything, I should be doing differently on this homework question?

Quote:Instructions
Program: print each word in a quote
start = 0
space_index = quote.find(" ")
while space_index != -1:
# code to print word (index slice start:space_index)

Output should look like below:

they
stumble
who
run
fast

I am given this (where the student continues to type below):
quote = # [ ] Print each word in the quote on a new line  
quote = "they stumble who run fast"
I add onto it (with the initial 2 given lines shown above):
# [ ] Print each word in the quote on a new line  
quote = "they stumble who run fast"
start = 0
space_index = quote.find(" ")
while space_index != -1:
    print(quote[start:space_index])
    start = space_index + 1
    space_index = quote.find(" ", space_index + 1)
My output is this:
they
stumble
who
run
SO, since there IS NOT a space (" ") after the word "fast" in the given statement, I don't know how else do render this correctly. Have I done all I can do correctly, but they just forgot to add a space (" ")?

Thank you for any input!
Reply
#2
Well, according to this site, find() method returns the lowest index of the substring if it is found in given string, and therefore, the way you use the find()must be wrong
See, since you have given the statement
space_index = quote.find(" ")
, the find() just finds instances till the spaces, so that is why your output is
Output:
they stumble who run
To get your desired output, there are two ways
  1. As you said you can add a space in front of your quote
    OR
  2. You can not use find() and instead use the split() method, which spits each word in the string
However, remember to consult your teacher of this problem or the course you are taking, and show him and if given approval, you can go ahead
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#3
Programming homework assignments are designed to demonstrate a problem and see how you go about solving the problem. This assignment has little to do with finding words in a sentence and a whole lot to do with finding the last word. The assignment practically solves everything except for how do you get the last word.

It is a demonstration of the fence post problem. You are building a split rail fence where two 8' rails span the space between two fence posts. You need to build an 80' long straight fence. How many posts and rails do you need? The railing part is really easy;2 * length / 8. The posts is a bit trickier. It's not 2 * length / 8 and it's not length / 8. It is something different, a "special case" or a "boundary condition" How are you going to solve your final word boundary condition? There are more ways than one.
Reply
#4
Thank you both!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what i am doing wrong ?( lbaskin3 3 1,354 Jun-09-2023, 08:49 AM
Last Post: Emilia4925
  im not sure what ive done wrong code doesnt run dgizzly 3 1,357 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  what is wrong with my code 53535 4 1,529 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 2,205 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  what am i doing wrong??? Himmank 2 2,091 Aug-10-2021, 03:16 AM
Last Post: deanhystad
  about write file wrong (Edit directly online) CNenfan 4 2,503 Jan-29-2021, 05:32 AM
Last Post: deanhystad
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 2,656 Dec-09-2020, 06:37 AM
Last Post: stranac
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,677 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Wrong output, something is missing in my codes Julan 5 2,384 Sep-28-2020, 02:34 PM
Last Post: perfringo
  So many things wrong, it runs but not like its supposed to kasherwood 6 2,678 Sep-26-2020, 04:57 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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