0

Making a text based game and want to read from the story text file via paragraph rather than printing a certain amount of characters?

You wake up from a dazed slumber to find yourself in a deep dank cave with moonlight casting upon the entrance...

You see a figure approaching towards you... Drawing nearer you hear him speak...
2
  • What does the text file look like? What have you tried so far? Commented Dec 5, 2018 at 21:48
  • 2
    What delimits one paragraph from another? Commented Dec 5, 2018 at 21:56

2 Answers 2

2

You want this: my_list = my_string.splitlines() https://docs.python.org/3/library/stdtypes.html#str.splitlines

Sign up to request clarification or add additional context in comments.

Comments

0

Like @martineau suggested you need a delimiter for separate different paragraphs. This can even be a new line character (\n) and after you have it you read all content of the file and split it by the defined delimiter. Doing so you generate a list of elements with each one being a paragraph. Some example code:

delimiter = "\n"
with open("paragraphs.txt", "r") as paragraphs_file:
    all_content = paragraphs_file.read() #reading all the content in one step
    #using the string methods we split it
    paragraphs = all_content.split(delimiter)

This approach has some drawbacks like the fact that read all the content and if the file is big you fill the memory with thing that you don't need now, at the moment of the story.

Looking at your text example and knowing that you will continuously print the retrieved text, reading one line a time could be a better solution:

with open("paragraphs.txt", "r") as paragraphs_file:
   for paragraph in paragraphs_file: #one line until the end of file
       if paragraph != "\n":
          print(paragraph)

Obviously add some logic control where you need it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.