热线电话:13121318867

登录
2018-12-06 阅读量: 807
如何在Python中的一行中输入用户的多个值?

在C中我们可以这样做:

// Reads two values in one line

scanf("%d %d", &x, &y)

一种解决方案是使用raw_input()两次。

x, y = raw_input(), raw_input()

请注意,我们不必显式指定split(''),因为split()使用任何空白字符作为分隔符作为默认值。

在上面的Python代码中要注意的一点是,x和y都是字符串。我们可以使用另一行将它们转换为int

下面是完整的一行代码,使用split和list comprehension从标准输入读取两个整数变量

# Reads two numbers from input and typecasts them to int using

# list comprehension

x, y = [int(x) for x in raw_input().split()]

# Reads two numbers from input and typecasts them to int using

# map function

x, y = map(int, raw_input().split())

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子