1
time=0
stop=input()

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

This is a program for an average speed camera. I was wondering whether it is possible for the while loop to stop when the user inputs "999". The value at which the code is broken would then be the new content of the time variable.

das-g
  • 9,718
  • 4
  • 38
  • 80
HELP
  • 1
  • 3
  • You might be looking for something like http://stackoverflow.com/questions/20576960/python-infinite-while-loop-break-on-user-input – das-g Nov 07 '15 at 16:35

2 Answers2

1

It's a bit unclear of what you're trying to accomplish, but based on the code you provided and your question, it sounds like you want to measure how long it takes for someone to enter a specific value. You can modify: 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

Then running it:

$ python guess_999.py
$ 6
$ 999
$ It took 2.765054 seconds
Community
  • 1
  • 1
Brock Hargreaves
  • 842
  • 6
  • 12
  • thanks Brock that's exactly what i was trying to accomplish. when i run the code on my idle it come up with a syntax error i have python 3.5 – HELP Nov 07 '15 at 17:32
0

EDIT: PO wanted to do something completely different. I refer to Mark's answer.

You messed it up a little bit ;)

Do:

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

Explanation: - input() will return a string of what the user typed into the console. What you write into the brackets is what will be written on the line when you are asked to type something. This is usually a question like "what's your name?" - if the answer is "999", the command break will be executed => loop stops

xXliolauXx
  • 1,273
  • 1
  • 11
  • 25
  • thank you xXliolauXx, however when i included your answer in my code the loop reiterated every time i inputted something, where as i wanted the loop to execute time+1 continuously until "999" was inputted – HELP Nov 07 '15 at 17:07
  • @HELP Oh I see what you wanted to accomplish now from Brock's answer... I think your description could have been a little bit clearer ;) – xXliolauXx Nov 08 '15 at 11:20