How to solve the issue of multiline input in Python3?

In Python3, multiple-line input functionality can be achieved using loops. The specific steps are as follows:

  1. Use an empty list to store the input of multiple lines of data.
  2. during
  3. Please provide input.
  4. Exit the loop after the user has finished inputting.
  5. remove any leading and trailing white spaces

Here is a sample code:

lines = []  # 用于存储输入的多行数据

while True:
    line = input()  # 接收用户输入的每一行数据
    if line.strip():  # 如果输入的内容不为空白行
        lines.append(line)  # 将输入的内容添加到列表中
    else:
        break  # 如果输入的内容为空白行,则退出循环

# 打印输入的多行数据
for line in lines:
    print(line)

By using the above code, you can receive multiple lines of input from the user and output the content of each line. The program will stop receiving input and output the results once the user enters a blank line.

bannerAds