2

I am trying to do a speed camera program by using a while loop to add up time. I want the user to be able to input "Enter" to stop the while loop with out the while loop pausing and waiting for the user input something, so the while loop works as a clock.

    import time
    timeTaken=float(0)
    while True:
        i = input   #this is where the user either choses to input "Enter"
                    #or to let the loop continue
        if not i:
        break
        time.sleep(0.01)
        timeTaken=timeTaken+0.01
    print(timeTaken)

I need a line of code which can detect whether the user has inputted something without using "input".

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
Max Dickson
  • 31
  • 1
  • 2
  • 1
    possible duplicate of [How do I check if stdin has some data?](http://stackoverflow.com/questions/3762881/how-do-i-check-if-stdin-has-some-data) – GreenAsJade Nov 07 '14 at 10:16
  • Well I just want the input inputted without pausing the program. – Max Dickson Nov 07 '14 at 10:16
  • Have a look at [this answer](http://stackoverflow.com/questions/292095/polling-the-keyboard-in-python). The _right_ keywords for a search are "python non blocking keyboard input" and more or less every finding refers to the `select` module. – gboffi Nov 07 '14 at 10:17
  • @MaxDickson «I just want the input ... without pausing» Think: when you're ready to process input you check if `stdin` has some data and either read the data without blocking (_pausing_) or you go on to do the other things you have to do. – gboffi Nov 07 '14 at 10:27
  • 1
    @GreenAsJade I had a look at your edit... isn't it possible to upvote an edit? I'be glad if I could do! excellent work! – gboffi Nov 07 '14 at 10:33
  • Are you interested about running the python programme and not bothering about pausing and input. It seems like you need to modify your executable and make sure that the command line argument contains your inputs which will be passed via sys.argv[]. – ha9u63a7 Nov 07 '14 at 10:38
  • @gboffi ... I guess you could always upvote the answer ;) – GreenAsJade Nov 07 '14 at 22:36
  • @GreenAsJade Thank you for the reminder, but I won't as long as there is that _double negation logic_ in your A. I mean: `if not no_input: ... `. OTOH, you could edit your answer... Ciao from – gboffi Nov 08 '14 at 07:41

2 Answers2

3

There are at least two ways to approach this.

The first is to check whether your "standard input" stream has some data, without blocking to actually wait till there is some. The answers referenced in comments tell you how to approach this. However, while this is attractive in terms of simplicity (compared to the alternatives), there is no way to transparently do this portably between Windows and Linux.

The second way is to use a thread to block and wait for the user input:

import threading 
import time

no_input = True

def add_up_time():
    print "adding up time..."
    timeTaken=float(0)
    while no_input:
        time.sleep(0.01)
        timeTaken=timeTaken+0.01
    print(timeTaken)


# designed to be called as a thread
def signal_user_input():
    global no_input
    i = raw_input("hit enter to stop things")   # I have python 2.7, not 3.x
    no_input = False
    # thread exits here


# we're just going to wait for user input while adding up time once...
threading.Thread(target = signal_user_input).start()

add_up_time()

print("done.... we could set no_input back to True and loop back to the previous comment...")

As you can see, there's a bit of a dilemma about how to communicate from the thread to the main loop that the input has been received. Global variable to signal it... yucko eh?

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • 2
    A question, the loop in `add_up_time` is written as you have written it for a reason, or it could be as well `while no_input: ...`? Thank you in advance. – gboffi Nov 07 '14 at 11:30
  • True :) I started from the orignal, and didn't optimise far enough eh? – GreenAsJade Nov 07 '14 at 11:35
-1

You should use a thread, if you are supposed to listen to input and have at the same time something else being processed.

user3666197
  • 1
  • 6
  • 50
  • 92
qstebom
  • 719
  • 4
  • 12
  • 1
    Okay I don't really know much about programming, doing for my GCSE so yeah I kind of need stuff explained for me. What do you mean? – Max Dickson Nov 07 '14 at 10:15
  • I mean that, in order for the while loop to continue you must run it in a separate thread if you also want to read from stdin. – qstebom Nov 07 '14 at 10:17