0
    for tweets in ExistingTweets:
    ExistString = tweets['text']
    ExistString = ExistString[:15]
    if randomQuote[:16] == ExistString:
        randomQuote = AllQuotes[randint(0,5)].getText()
        randomQuote = randomQuote[printUntil:]

I am trying to see if the quote I am about to tweet has already been tweeted. The ExistString and randomQuote match up once(I am aware I'm calling :16 in the randomQuote, but somehow :15 and :16 equal to the same output) , but randomQuote doesn't change!

Any help is appreciated, thank you!

T Bruijnen
  • 13
  • 4
  • In python, if you try to slice past the end of the list it will return the slice until the end of the list. That's probably why you are getting the same output using `:15` and `:16`. – Carles Mitjans Apr 20 '17 at 09:06
  • 1
    @CarlesMitjans I was getting the same output because randomQuote had one character more at the start... Silly mistake! – T Bruijnen Apr 20 '17 at 09:59

1 Answers1

0

The structure of your code is a little confusing (I guess you haven't indented it correctly?) and I'm not sure what some stuff like printUntil is meant to do, but what I'd suggest would be something like this:

randomQuote = AllQuotes[randint(0,5)].getText()    # Pick your first random quote
while True:
    already_used = False
    for tweets in ExistingTweets:
        if randomQuote[:15] == tweets['text'][:15]:
            already_used = True
            break
    if already_used:
        randomQuote = AllQuotes[randint(0,5)].getText() # Pick another and try again.
    else:
        break

print(randomQuote)

This would be significantly easier if you just had a big list of the text of the quotes you'd already tweeted; then you could just go if randomQuote in ExistingTweets as your test, and wouldn't need the for loop at all.

As another commenter said, you shouldn't be using different values ([:15] and [:16]) - they may give the same result in some specific cases (where the string is shorter than 17 characters for example) but they won't in other cases.

RobF
  • 221
  • 2
  • 8
  • Thanks for the quick answer. This makes way more sense already! However... It still doesn't work! I've put two print statements after the for statement, to print both the tweet and randomquote. It prints the first 15 characters for the tweet, but the first 14 for the randomquote! I used different numbers too, same thing. Randomquote always seem to be one character short. Any ideas?! – T Bruijnen Apr 20 '17 at 09:39
  • Well, figured it out, the printUntil added a space to string... I sliced one character short! Beginner mistake hahaha. Thank you for your answer, I appreciate it! – T Bruijnen Apr 20 '17 at 09:58