为什么我的推特机器人只发一个词或一个字母,而不是一个完整的句子?

Why does my twitter bot only tweet one word or letter, and not a full sentence?

我编写了随机句子生成器,它可以工作。但是,当它发出推文时,它只会推一个词或一个字母。我真的不知道发生了什么。这是句子和推文的代码:

#random sentence generator
with open("Hannibal.txt") as f:
    words = f.read().split()

word_dict = defaultdict(list)
for word, next_word in zip(words, words[1:]):
    word_dict[word].append(next_word)

word = "Hannibal"

while not word.endswith("."):
    print(word, end=" ")
    word = random.choice(word_dict[word])
    if len(word) > 50:
        word = word[-5:]
print(word)

#this bit is supposed to tweet the random sentence.
for i in word:
    try:
        print("Status Updated!")
        print(word)
        api.update_status(i)
        time.sleep(1200)
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break

这是我 运行 代码时打印出来的内容。

#第一句: 汉尼拔迷失了,此外,在他自己的儿子掌管的地方,士兵们现在遍布城墙,剥夺了汉尼拔在意大利各个地区的野心,但他不得不说,他带着他的同事喜欢让他们认为那些愤怒的时代,在随后发生的一切持续了十七年之后,迦太基人愿意避开人质或关闭,并忠于他们的战斗。

#然后这个通知: 状态已更新!

#Finally this 这只是句子的最后一个词: 战斗

这条推文只是那个词的第一个字母。

here is what is tweeted

每次添加新的随机单词时,您似乎都在覆盖“单词”变量:

word = random.choice(word_dict[word])

相反,你应该做类似的事情

sentence = []
word = "Hannibal"
while not word.endswith("."):
    sentence.append(word)
    word = random.choice(word_dict[word])
    ...
sentence = " ".join(sentence)