魔术8球TXT文件列表

发布于 2025-01-19 01:05:51 字数 803 浏览 0 评论 0原文

我需要我的随机响应是我存储在 txt 文件中的句子或字符串。 它只打印出一个单词而不是整行,我该如何解决这个问题,谢谢。

import random
import time
question = input("Ask the magic 8 ball a question (press enter to quit): ")  
while question:
    if question == "quit":
        break
    print("Let's see what your future holds")
    print("Let me check real quick")
    for i in range(random.randrange(3,10)):
        print(". ",end=" ")
        time.sleep(1)
    print("\nYour answer is")
    print("Your future is " + (random.choice(open("responses.txt","r").readline().split())) + " .")
    question = input("Ask the magic 8 ball a question (press enter to quit): ")

print("See ya")

响应.txt: 可以肯定的是 无疑 你可以信赖它 是的,绝对是 确实如此 据我看来,是的 最有可能的 是的 前景良好 迹象表明是的 回复朦胧再试一次 现在最好不告诉你 稍后再询问 现在无法预测 集中精力再问一次 不要指望它 前景不太好 我的消息来源说不 非常怀疑 我的回答是不

I need my random responses to be the sentences or strings I have stored in my txt file.
It only prints out a single word instead of the whole line, how can I fix this, thanks.

import random
import time
question = input("Ask the magic 8 ball a question (press enter to quit): ")  
while question:
    if question == "quit":
        break
    print("Let's see what your future holds")
    print("Let me check real quick")
    for i in range(random.randrange(3,10)):
        print(". ",end=" ")
        time.sleep(1)
    print("\nYour answer is")
    print("Your future is " + (random.choice(open("responses.txt","r").readline().split())) + " .")
    question = input("Ask the magic 8 ball a question (press enter to quit): ")

print("See ya")

response.txt:
It is certain
Without a doubt
You may rely on it
Yes definitely
It is decidedly so
As I see it, yes
Most likely
Yes
Outlook good
Signs point to yes
Reply hazy try again
Better not tell you now
Ask again later
Cannot predict now
Concentrate and ask again
Don’t count on it
Outlook not so good
My sources say no
Very doubtful
My reply is no

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2025-01-26 01:05:51

正确的方法是

print("Your future is " +(random.choice(open("responses.txt","r").read().splitlines())) + " .")

解决您的问题

The correct way to do it is

print("Your future is " +(random.choice(open("responses.txt","r").read().splitlines())) + " .")

This should solve your problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文