📌  相关文章
📜  在Python中从用户那里获取多个输入

📅  最后修改于: 2022-05-13 01:55:36.544000             🧑  作者: Mango

在Python中从用户那里获取多个输入

开发人员经常希望用户在一行中输入多个值或输入。在 C++/C 中,用户可以使用 scanf 在一行中获取多个输入,但在Python中,用户可以通过两种方法在一行中获取多个值或输入。

  • 使用 split() 方法
  • 使用列表理解

使用split()方法:
此函数有助于从用户那里获得多个输入。它通过指定的分隔符打破给定的输入。如果未提供分隔符,则任何空格都是分隔符。通常,用户使用 split() 方法来拆分Python字符串,但可以使用它来获取多个输入。

句法 :

input().split(separator, maxsplit)

例子 :

Python3
# Python program showing how to
# multiple input using split
 
# taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
print()
 
# taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
print()
 
# taking two inputs at a time
a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))
print()
 
# taking multiple inputs at a time
# and type casting using list() function
x = list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", x)


Python3
# Python program showing
# how to take multiple input
# using List comprehension
 
# taking two input at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print()
 
# taking three input at a time
x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
print()
 
# taking two inputs at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))
print()
 
# taking multiple inputs at a time
x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x)


Python3
# taking multiple inputs at a time separated by comma
x = [int(x) for x in input("Enter multiple value: ").split(",")]
print("Number of list is: ", x)


输出:

使用列表理解
列表推导式是在Python中定义和创建列表的一种优雅方式。我们可以像数学语句一样只在一行中创建列表。它还用于从用户获取多个输入。

例子:

Python3

# Python program showing
# how to take multiple input
# using List comprehension
 
# taking two input at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print()
 
# taking three input at a time
x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
print()
 
# taking two inputs at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))
print()
 
# taking multiple inputs at a time
x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x)

输出 :

注意:以上示例采用空格分隔的输入。如果我们希望输入用逗号 (, ) 分隔,我们可以使用以下内容:

Python3

# taking multiple inputs at a time separated by comma
x = [int(x) for x in input("Enter multiple value: ").split(",")]
print("Number of list is: ", x)

有关示例运行,请参阅 https://ide.geeksforgeeks.org/BHf0Cxr4mx。