我想知道是否可以在一行标准输入中输入两个或多个整数。在 C
/ C++
很简单:
C++
:
#include <iostream>
int main() {
int a, b;
std::cin >> a >> b;
return 0;
}
C
:
#include <stdio.h>
void main() {
int a, b;
scanf("%d%d", &a, &b);
}
在 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'
那怎么办呢?
原文由 enedil 发布,翻译遵循 CC BY-SA 4.0 许可协议
在空白处拆分输入的文本:
演示: