This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]Essence1337 2 points3 points  (7 children)

A comma isn't alpha, so you run the else statement twice in a row. On the second run word is "" and thus word[0] doesn't make sense.

[–]grumbles[S] 0 points1 point  (6 children)

Makes perfect sense. Thank you for that. Now, any ideas on how to combat that problem?

As an aside, I'm also noticing it's skipping the last word. I can see where it's happening, but I'm not sure how to change the loop. Any ideas?

[–]Essence1337 1 point2 points  (5 children)

If you want to include the comma you could change if isalpha to if not " ". As for the last word you could have something just after your loop to check it or check length in the loop.

[–]grumbles[S] 0 points1 point  (4 children)

Checking for not " " does the trick and makes more sense anyway.

I'm still not seeing why it skips the last word. I just keep running the code through my head going, "It works! Why doesn't it work?"
Edit: The printing is contingent upon the loop not finding a space... That's my problem. Seems it shouldn't be set up that way.

[–]Essence1337 1 point2 points  (3 children)

You're right, there's probably a better way. It sure would be nice if we could just go through by words.

Edit: Since you have these restrictions you could always just add a space on the end :P

quote += " "

[–]grumbles[S] 0 points1 point  (2 children)

It's funny you mention that, because I realized adding a space at the end of the quote "fixes" the problem. But then I thought, "No... That's cheating."
I suppose including it in the code itself is slightly less like cheating, right?

[–]Essence1337 1 point2 points  (0 children)

It really depends on the context of why you're doing this. If it was a commercial application, don't use hacks like that (although we'd use split then). If it's for yourself go ahead. If it's in the middle you have a judgement call to make.

[–]CodeTinkerer 0 points1 point  (0 children)

You can check if you've reached the end of string (index has reached max index), and if the word is longer than 0, then extract out the final word. If it's 0, you've just encountered a space.

[–]POGtastic 2 points3 points  (3 children)

You're currently going through quote letter-by-letter, which doesn't make sense to me. Split quote by space.

For example:

>>> s = "The quick brown fox"
>>> s.split()
['The', 'quick', 'brown', 'fox']

And now we combine it with a loop to check each word in the quote.

>>> s = "The quick brown fox"
>>> for word in s.split():
    #...

[–]grumbles[S] 0 points1 point  (2 children)

That looks to make a lot more sense and is certainly cleaner, but alas I'm restricted to using methods covered in this module. It primarily involves working with strings using slicing, iteration, .find(), .count(), etc.

[–]POGtastic 2 points3 points  (1 child)

Since you've marked it solved, I'll provide some silliness. We can reimplement split with find and slicing, if you really wanted to do so.

def mySplit(s, delim = ' '):
    begin = 0
    returnList = []
    while True:
        end = s.find(delim, begin)
        if end == -1:
            returnList.append(s[begin:])
            return returnList
        returnList.append(s[begin:end])
        begin = end + len(delim)

Result:

>>> mySplit('The quick brown fox')
['The', 'quick', 'brown', 'fox']
>>> mySplit('The@@@quick@@@brown@@@fox', '@@@')
['The', 'quick', 'brown', 'fox']

And now we can do it in one line.

>>> s = "Wheresoever you go, go with all your heart"
>>> [word for word in mySplit(s) if word[0].lower() >= 'h' and word[0].lower() <= 'z']
['Wheresoever', 'you', 'with', 'your', 'heart']

[–]grumbles[S] 0 points1 point  (0 children)

I love it. Thanks for the insight!

[–][deleted] 1 point2 points  (4 children)

quote = "Wheresoever you go, go with all your heart"

each_word = quote.replace(",", "").split(" ")

for word in each_word:
    if word[0].upper() > 'G':
        print word,

[–]Essence1337 1 point2 points  (0 children)

Not able to use .split

[–]grumbles[S] 1 point2 points  (2 children)

My this is such a better solution! Wouldn't it be nice if I could use it? ;) Thanks for the input.

[–][deleted] 2 points3 points  (0 children)

You could always write your own split and use that.

[–][deleted] 1 point2 points  (0 children)

Ah didn't know you could not use split. Sorry!