我加入了黑暗面并决定学习Python。我使用的是python3。在
这是一个直接向前的方法,用C++一次读取两个整数,直到它们都是0:
int x, y;
while (cin >> x >> y && (x != 0 || y != 0)) {
//x or y can be 0 but not both
}
//now x = 0 and y = 0 OR can't find two int's
它简单易行,99.999%的时间都能用。我在Python中有以下内容,但对我来说它似乎不是Python。此外,这在某些输入上注定会失败(例如,如果int在两个不同的行上)
^{pr2}$有人能告诉我使用python3一次读取两个int直到两个int都为0的最干净的Python方法吗?在
对于python2.x,您将希望使用^{} ,而对于python3.x,您只需使用^{}
示例代码几乎是合理的,只是它可以立即解包变量并使用异常处理来处理错误
试试这个。我的简单测试似乎有效。但是,如果您只键入一个数字,它将抛出一个错误。在
这个版本正确地处理了“找不到2个整数”的情况。在
^{pr2}$相关问题 更多 >
编程相关推荐