我需要我的随机响应是我存储在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:

推荐答案

正确的方法是

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

这应该能解决你的问题

Python-3.x相关问答推荐

我的SELECT函数搜索的是列,而不是列中的数据.我怎么才能让它搜索数据呢?

Pandas 数据帧断言等同于NaN

Python将类实例变量转换为嵌套 struct

如何获取自定义文件上传路径的对象ID?

我想使用命令提示符安装 cv2

在新数据帧上自动提取两个字符串 python 之间的相等性

在 python 中使用正则表达式在行尾查找特定元素

如何将搜索结果中的所有值保存在另一个列表中?

在 pytest 中,如何测试 sys.exit('some error message')?

删除给定数组中所有元素为True的所有子数组

使用 RANSAC 在激光雷达点云中查找电力线

python3:字节与字节数组,并转换为字符串和从字符串转换

pandas 中 df.reindex() 和 df.set_index() 方法的区别

ValueError:预期的 2D 数组,得到 1D 数组:

在没有时间的python中创建日期

如何在 python 3.x 中禁用 ssl 判断?

如何使用 Python 订阅 Websocket API 通道?

计算两个文件的行差异的最有效方法是什么?

什么是ANSI_X3.4-1968编码?

Python:如何在 Windows 资源管理器中打开文件夹(Python 3.6.2、Windows 10)