-2

I want it so that my code continuously replaces itself continuously, and while that executes, I want the input to just take what it is without needing to press enter. I'm an android user and I've already tried downloading non-built-in-modules, so I can't use the keyboard module or anything like that.

My code is basically:

while game == "on":
    print("string")
    inpu = input
    time.sleep(1)
    clear screen

Apologies for false text editing or bad "code grammar." I'm new to coding and stack overflow.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Drew
  • 1
  • Why do you have what your code "is basically" and not a minimal reproducible example showing the behavior your code is having? Also, please specify what language you are using and add the relevant tag. – blackbrandt Oct 15 '22 at 16:07
  • @blackbrandt well, the language is python, as the title says. also the code is print("Health - " + healthS + healthSI) print("Hunger - " + hungerS + healthSI), time.sleep(0.2), cls() and cls() is already defined – Drew Oct 15 '22 at 23:20
  • Welcome to Stack Overflow. There isn't really a canned, built-in solution for this; you are either going to need a third-party library (and learn to use it) or do considerable work yourself with threading. – Karl Knechtel Oct 15 '22 at 23:58
  • Welcome to Stack Overflow! Please take the [tour]. I'm not totally sure what you're asking. It sounds like you want the user to type something in, then when your code loops, it'll take whatever they put, is that right? Why can't you use the keyboard module exactly? For more tips, see [ask]. – wjandrea Oct 15 '22 at 23:59
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 16 '22 at 01:20

1 Answers1

0

NOT wait for user input while in a loop?

Solution

That is easy, using multi-thread or multi-process. i.e create another thread that handles the input separately.

Demo of full code

Here is a very simple demo of multi-thread to begin.

import _thread

args = None

# Define a function for the thread
def mythread(threadname):
   global args
   args = input("input anything: ")

# Create one threads as follows
try:
   _thread.start_new_thread( mythread, ("UI",) )
   while args==None:  # mimic your work loop
       pass;     
   print("main loops end demo end")
except:
   print ("Error: unable to start thread")

  • To run

    $ python3 demo.py
    
  • Output

    input anything: hello
    main loops end demo end
    
Xin Cheng
  • 1,432
  • 10
  • 17