Python Program: Print Words Alternately in Separate Lines
n = int(input()) message = input()
Split the message into words
words = message.split()
Calculate the number of words per line
words_per_line = len(words) // n
Initialize a variable to keep track of the current word index
current_word_index = 0
Print the words alternately in n separate lines
for i in range(n): # Initialize a variable to store the words for the current line line_words = []
# Add the words to the line_words list
for j in range(words_per_line):
line_words.append(words[current_word_index])
current_word_index += 1
# Print the line_words as a string
print(' '.join(line_words))
Print any remaining words
if current_word_index < len(words): remaining_words = words[current_word_index:] print(' '.join(remaining_words))
原文地址: https://www.cveoy.top/t/topic/pc8E 著作权归作者所有。请勿转载和采集!