是否可以使用 python 中的用户输入来实现 break 语句

Is it possible to implement a break statement using a user input in python

time=0
stop=input()

while time<1000000000000000000000000000000000000000000000000000:
    if stop==input("999"):
        break
    print (time)
    time= time+1
print("time taken is",time) 

这是一个用于平均测速相机的程序。我想知道当用户输入“999”时 while 循环是否有可能停止。代码被破坏的值将是时间变量的新内容。

编辑: PO 想要做一些完全不同的事情。我参考了马克的回答。

你有点搞砸了 ;)

做:

answer = input("type something")
if  answer == "999":
    break

说明: - input() 将 return 用户在控制台中输入的字符串。您在括号中写的是当您被要求输入内容时将写在该行上的内容。这通常是 "what's your name?" 这样的问题 - 如果答案是“999”,将执行命令 break => loop stops

有点不清楚您要完成什么,但根据您提供的代码和您的问题,您似乎想衡量某人输入特定值需要多长时间。可以修改:Python - Infinite while loop, break on user input:

#guess_999.py
import sys
import os
import fcntl
import time

fl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, fl | os.O_NONBLOCK)

time_started = time.time()
while True:
    try:
        stdin = sys.stdin.read()
        if "999" in stdin:
            print "It took %f seconds" % (time.time() - time_started)
            break
    except IOError:
        pass

然后运行它:

$ python guess_999.py
$ 6
$ 999
$ It took 2.765054 seconds