-2

How do I run two functions at the same time?

Consider the following example code:

from time import sleep
userinps = []
def inp():
    while True:
         userinp = input("Your input: ")
         userinps.append(userinp)

def outp():
    while True:
         print("Your inputs: " + str(userinps))
         sleep(10)

How do I run both the functions inp() and outp() at the same time? I want to accept user input and print text every random amount of time in my project.

Also how do I clear the text that the user inputted and the the string "Your Input: " (after pressing enter)?

martineau
  • 112,593
  • 23
  • 157
  • 280
  • 1
    You might be looking for [`threading`](https://docs.python.org/3/library/threading.html). – Matthias May 27 '22 at 08:18
  • how do i use threading @Matthias – sad mountains noise May 27 '22 at 08:19
  • @TDG yes, but i also want to know how i can get rid of the text the user inputted? look at the last line in my question – sad mountains noise May 27 '22 at 08:25
  • Controlling the output in a command line isn't easy. Maybe you should ask a different question for that. – Matthias May 27 '22 at 08:26
  • @TDG I just noticed i got an error and Your Input: doesnt show up the second time the while loop is started. the error is: `Your input: Your inputs: [] Process Process-1: Traceback (most recent call last): File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap self.run() File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run self._target(*self._args, **self._kwargs) File "main.py", line 8, in inp userinp = input("Your input: ") EOFError: EOF when reading a line` also it doesnt append to the list `userinps`? i even tried global – sad mountains noise May 27 '22 at 08:36
  • "how do i use threading"? — by reading the docs and/or one of the thousands of tutorials there are about the subject. – martineau May 27 '22 at 09:49
  • Also see [How can I use threading in Python?](https://stackoverflow.com/questions/2846653/how-can-i-use-threading-in-python) – martineau May 27 '22 at 10:15

0 Answers0