在Python中有办法做到这一点吗?(我知道你可以做到Java。)
#PSEUDO CODE!!
while (inp = input()) != 'quit'
print(inp)在java中,上面的伪代码翻译为:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
String inp;
while (!(inp = reader.readLine()).equals("quit")) {
System.out.println(inp);
}
} catch (IOException io) {
System.out.println(io.toString());
}编辑:
..。回答..。但这是唯一的办法吗?
while True:
inp = input()
if inp == 'quit':
break
print(inp)
print('eof')发布于 2014-07-22 16:25:45
编辑:
在这种情况下,您可以使用for-循环和iter。
for inp in iter(input, 'quit'):
print(inp)iter(input, 'quit')将继续调用input函数并将其返回值分配给inp,只要该值不等于'quit'。
不,您不能在Python中执行内联分配。语法根本不允许这样做(请记住,赋值是Python中的语句)。
但是,您可以这样做:
while True: # Loop continuously
inp = input() # Get the input
if inp == 'quit': # If it equals 'quit'...
break # ...then break the loop
print(inp) # Otherwise, continue with the loop这大致相当于你想要做的事。
发布于 2014-07-22 16:28:53
您可以在循环之前定义inp,并在时间内重新分配:
inp = None
while inp != 'quit':
print(inp)
inp = input()如果您想在进入want循环之前退出,请先设置inp = input()。
inp = input()
while inp != 'quit':
print(inp)
inp = input()https://stackoverflow.com/questions/24892746
复制相似问题