【发布时间】:2018-07-26 23:38:40
【问题描述】:
我想在 python 中进行用户输入,类似于 c++ 中使用的 getchar() 函数。
c++代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char ch;
while(1){
ch=getchar();
if(ch==' ') break;
cout<<ch;
}
return 0;
}
输入:堆栈溢出
输出:堆栈
在上面的代码中,当用户输入空格时,循环中断。 我想在 python 中使用我在 c++ 代码中使用的 getchar() 类型函数来执行此操作。
【问题讨论】:
-
stackoverflow.com/questions/510357/… 我见过这个,但我需要python 3中的内置函数方式。为了更清楚我的问题,我给出了c++代码。
-
那么您的问题标题应该类似于 “Python 3 中是否有像 C++ 中的 getchar() 这样的内置函数?”。无论如何,似乎可能的答案就在那个原始问题中。
-
@milton。见我的answer。第二种方法(使用 STDIN)是使用内置函数。
-
@codelt 非常感谢!它清除了我