How can we input multiple integers from the keyboard in Python?

In Python, you can use the input() function to enter data from the keyboard, then split() the input string into multiple integers, and finally store them in a list.

Here is a sample code:

numbers = input("请输入若干个整数,以空格分隔:")
numbers_list = numbers.split()  # 将输入的字符串按空格分割为多个字符串
numbers_list = [int(num) for num in numbers_list]  # 将字符串列表转换为整数列表

print(numbers_list)

After running the code, the program will prompt you to enter several integers separated by spaces. For example, entering 1 2 3 4 5 will result in the output [1, 2, 3, 4, 5].

bannerAds