简体   繁体   中英

How to write function that takes in a string message and returns a list of string messages with pagination

I want to write a function that takes in a string message and returns a list of string messages with pagination if needed.

This is what I have

import textwrap

def smart_collect():
    text = input('Please Enter a text: ')
    dedented_text = textwrap.dedent(text).strip()
    wrap_text = textwrap.wrap(dedented_text, width=212)
    max_page = len(wrap_text)

    for i in range(0, max_page):
        print(f'{wrap_text[i]} ({i+1}/{max_page})')


smart_collect()

Input Text/String:

As a reminder, you have an appointment with Dr. Smith tomorrow at 
3:30 pm. If you are unable to make this appointment, please call our 
customer service line at least 1 hour before your scheduled 
appointment time.

My Output:

As a reminder, you have an appointment with Dr. Smith tomorrow at(1/1)
3:30 pm. If you are unable to make this appointment, please call our 
3:30: command not found
customer service line at least 1 hour before your scheduled customer: 
command not found
appointment time.

Expected Results

As an example, the following message has 212 characters:

As a reminder, you have an appointment with Dr. Smith tomorrow at 
3:30 pm. If you are unable to make this appointment, please call our 
customer service line at least 1 hour before your scheduled 
appointment time.

The message should be sent in two chunks as such:

As a reminder, you have an appointment with Dr. Smith tomorrow at 
3:30 pm. If you are unable to make this appointment, please call our 
customer service  (1/2)

and

line at least 1 hour before your scheduled appointment time. (2/2)

You can't use textwrap.fill() here for your purposes:

Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph.

Instead, you need to use textwrap.wrap()

Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

Since it already returns a list, there isn't much left for you to do here.

def smart_collect():
    text = input('Please Enter a text: ')
    dedented_text = textwrap.dedent(text).strip()
    wrap_text = textwrap.wrap(dedented_text, width=100)
    return wrap_text

print(smart_collect())

Now that you have a list of strings that are width=100 characters long, you can now do whatever it is you want with your strings. For example, if you wanted to print them you would do:

for each in smart_collect():
    print(each)

Now if you wanted to add pagination, you could do this:

list_strings = smart_collect()
max_page = len(list_strings)
for i in range(0, max_page):
    print(f'{list_strings[i]} ({i+1}/{max_page})')

For your input, your result (with width=100) looks like this:

As a reminder, you have an appointment with Dr. Smith tomorrow at 3:30 pm. If you are unable to make (1/3)
this appointment, please call our customer service line at least 1 hour before your scheduled (2/3)
appointment time. (3/3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM