0

I'm new in python. I'm writing a code using input() in while loop to get a char in console, but it stops when the input() function is called (actually it is waiting an input). I want to run the loop regardless of the input, and code should be like this:

while True:
    tmp = '0'
    if (console.readable()):
        tmp = input()

    ... #some tasks

This code is like the way I did with C for serial connection (readable() checks if something is in console buffer). I'm not sure even the 'console buffer thing' exists, or maybe there's another way to use callback function. I hope someone knows how to do this in python. Thanks!!

  • That dupe seems to answer this question even though it's about something slightly different. – Carcigenicate May 01 '18 at 13:35
  • @Carcigenicate After reading the answers in the dupe, just out of curiosity: couldn't you just do a `if __name__ == "__main__":` in the loop? – patrick May 01 '18 at 13:41
  • @patrick I'm not sure what you mean you mean. `if __name__ =="__main__":` has nothing to do with taking console input. – Carcigenicate May 01 '18 at 13:44
  • My thought was to run the `input` part only when the script is called from the shell rather than imported etc. But maybe I misunderstood the question... – patrick May 01 '18 at 14:04
  • @patrick The "but it stops when the input() function is called (actually it is waiting an input). I want to run the loop regardless of the input" and `if (console.readable()): tmp = input()` bit suggests they want to call `input` only if they know that it won't block. – Carcigenicate May 01 '18 at 14:08
  • @Carcigenicate Thank you for your help. The link is what i want to do. – takattaka May 01 '18 at 14:17
  • @takattaka No problem. And if it answers your question, you should accept the duplicate suggestion to mark your question as answered. – Carcigenicate May 01 '18 at 14:18

0 Answers0