热线电话:13121318867

登录
2019-01-18 阅读量: 956
python如何获取多个输入?

问题描述:

在C ++ / C中,用户可以在一行中使用多个输入,但在Python中,如果没有设置,往往是一段代码输出一行结果。

问题解决:

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

句法 :

input()。split(separator,maxsplit)

# Python program showing how to

# multiple input using split

# taking two inputs at a time

x, y = input("Enter a two value: ").split()

print("Number of boys: ", x)

print("Number of girls: ", y)

print()

# taking three inputs at a time

x, y, z = input("Enter a three value: ").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 a two value: ").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 a multiple value: ").split()))

print("List of students: ", x)

132.3500
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子