How do you input multiple numbers in Python?

In Python, you can use the input() function to receive user input. If you need to input multiple numbers, you can separate them using spaces, commas, or other separators. Then use the split() method to split the input string into multiple numbers and store them in a list.

Here is an example code:

user_input = input("请输入多个数字,以空格分隔:")

numbers = user_input.split()  # 使用默认的空格作为分隔符,将输入的字符串分割成多个数字

# 将字符串转换为数字
numbers = [int(num) for num in numbers]

print("输入的数字列表:", numbers)

Example output:

请输入多个数字,以空格分隔:1 2 3 4 5
输入的数字列表: [1, 2, 3, 4, 5]

Please note that if commas or other separators are used between the input numbers, you can specify the corresponding separator in the split() method, for example split(‘,’), and if the input is a decimal, you can use the float() function to convert the string to a float.

bannerAds