0

How can I print directly after my input without waiting until the user answered the input statement?

def InputSaveName():
    try:
        import os, sys, time, pickle, colorama
    except Exception as e:
        print("Some modules are mssing! Install them and try again! {}".format(e))
    colorama.init()
    print("+----------------------+")
    print("What is your name adventurer?")
    name = input("> ")
    print("+----------------------+")

I want the bottom line to print without waiting for the user to put something in the input statement. In short: I want the code to run simultaneously.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
MdeGraaff
  • 122
  • 1
  • 11
  • 1
    What is the point of ```input()``` if you print something and the cursor moves forward without taking any input? – Abhinav Mathur Oct 21 '20 at 09:05
  • I'm making a game but there is one line under the input and I want to print that at the same time as the input gets printed, I'll edit the code – MdeGraaff Oct 21 '20 at 09:08
  • You need threads for this purpose – Abhinav Mathur Oct 21 '20 at 09:11
  • Does this answer your question? [Running two lines of code in python at the same time?](https://stackoverflow.com/questions/20260547/running-two-lines-of-code-in-python-at-the-same-time) – Abhinav Mathur Oct 21 '20 at 09:13
  • 3
    This seems to be an XY problem. You do not really want to run multiple lines of code at once. Instead, you want to build a complex full-screen terminal application. You should have a look at [`curses`](https://docs.python.org/3/howto/curses.html) – tobias_k Oct 21 '20 at 09:14

4 Answers4

4

This seems to be an XY problem. You do not really want to use threading to run multiple lines of code at once. To build a complex full-screen terminal application, you should have a look at curses:

import curses

def getname(stdscr):
    stdscr.clear()
    
    stdscr.addstr(0, 0, "+---------------------------+")
    stdscr.addstr(1, 0, "What is your name adventurer?")
    stdscr.addstr(2, 0, "> ")
    stdscr.addstr(3, 0, "+---------------------------+")
    
    curses.echo()
    return stdscr.getstr(2, 3, 20)

s = curses.wrapper(getname)
print("Name is", s)

This only asks for the name and then returns, but you can also add lines, or replace existing lines on the existing screen and refresh the screen.

tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

Without being 100% sure if it works in the specific examples you have typed in your question because of access to the standard output.

If you want to run things in parallel you can read about threads / subprocess https://docs.python.org/3/library/subprocess.html

or fork / multiprocessing https://docs.python.org/3/library/multiprocessing.html

EDIT after op's EDIT ;-)

What you want to do seems very similar to what's described in this question Python nonblocking console input

AsTeR
  • 7,247
  • 14
  • 60
  • 99
0

This might be what you are looking for. There is a "background process" running while waiting for your input using two separate threads.

import time
import threading

def myInput():
    print("Type your name when ready!")
    name = input()
    print("Your name is: ", name)

def backgroundProcess():
    while (True):
        print("Some code is running...")
        time.sleep(1)


inputThread = threading.Thread(target=myInput)
processThread = threading.Thread(target=backgroundProcess)

inputThread.start()
processThread.start()
rrswa
  • 1,005
  • 1
  • 9
  • 23
-1

You can use threading or multiprocessing modules:

import threading

def makeAsomethingOne():
    print('I am One!')

def makeAsomethingTwo(printedData=None):
    print('I am Two!',printedData)

name = input("> ")

firstThread = threading.Thread(target=makeAsomethingOne)
twiceThread = threading.Thread(target=makeAsomethingTwo,args=[name])

firstThread.start()
twiceThread.start()

This code runs almost simultaneously.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
luckyluke
  • 17
  • 3
  • 1
    The program will get blocked by the `input` and the threads are never created... This makes no sense at all – Tomerikoo Oct 21 '20 at 09:20
  • yuri => please control of your python version;threading or multiprocessing modules is default on python 3 – luckyluke Oct 21 '20 at 09:24
  • did you actually run this code and got a printed line **below** the input prompt? I hardly doubt that... – Tomerikoo Oct 21 '20 at 09:27
  • "How can I print directly after my input", "In short: I want the code to run simultaneously." => Tomerikoo – luckyluke Oct 21 '20 at 09:29
  • This code doesn't run simultaneously - it gets blocked by the `input`. You take input and then just print two lines from two different threads... This is not what is asked for and the fact that this is accepted puzzles me – Tomerikoo Oct 21 '20 at 09:30
  • The OP wants to print after the input prompt but **before** the user puts any input. Your program simply prints after the input is given, it would be just the same to **NOT** use threads and just print... Your code is equivalent to: `name = input("> ") ; print('I am One!') ; print('I am Two!', name)` – Tomerikoo Oct 21 '20 at 09:31