相关疑难解决方法(0)

如何在Python中的一行中输入2个整数?

我想知道是否可以在一行标准输入中输入两个或更多整数.在C/ C++它很容易:

C++:

#include <iostream>
int main() {
    int a, b;
    std::cin >> a >> b;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

C:

#include <stdio.h>
void main() {
    int a, b;
    scanf("%d%d", &a, &b);
}
Run Code Online (Sandbox Code Playgroud)

Python,它将无法工作:

enedil@notebook:~$ cat script.py 
#!/usr/bin/python3
a = int(input())
b = int(input())
enedil@notebook:~$ python3 script.py 
3 5
Traceback (most recent call last):
  File "script.py", line 2, in <module>
    a = int(input())
ValueError: invalid literal for int() with base 10: '3 5'
Run Code Online (Sandbox Code Playgroud)

那怎么办呢?

python input line python-3.x

9
推荐指数
2
解决办法
3万
查看次数

标签 统计

input ×1

line ×1

python ×1

python-3.x ×1