如何在用户决定结束之前读取输入

时间:2015-03-04 15:27:11

标签: python python-2.7 python-3.x

我如何在python中编写一个与以下内容完全相同的代码:

while(cin>>a){//some operations on "a"}

在c ++中?我试试:

while(a=raw_input()):

但是IDLE会在“=”上引发语法错误。我不想用

while True:

3 个答案:

答案 0 :(得分:5)

使用带有标记的迭代器:

for a in iter(raw_input, ""):
    # do stuff with a

这将继续运行iterraw_input())的第一个参数,直到结果(a)等于第二个参数("")。这样,当用户按下回车键时,循环终止。

答案 1 :(得分:0)

a = ' '
while a:  # loop exits when user just hits <ENTER>
    a=raw_input()
    # do stuff with a

答案 2 :(得分:0)

语法错误IDLE给出&#39; =&#39;在条件陈述中,它必须是&#39; ==&#39;,如

while a == raw_input(''):
    code code code