Python如何在不重写多线程时输入的内容的情况下打印

Python如何在不重写多线程时输入的内容的情况下打印,python,python-3.x,code-formatting,Python,Python 3.x,Code Formatting,我正在尝试使用python制作类似聊天的应用程序,但如果对方在我键入内容时发送文本,它就会出错。我的代码如下: import threading import time import sys def printf(str, *args): print(str % args, end='') def printwait(): global End while not End: time.sleep(3) print(' ')

我正在尝试使用python制作类似聊天的应用程序,但如果对方在我键入内容时发送文本,它就会出错。我的代码如下:

import threading
import time
import sys

def printf(str, *args):
    print(str % args, end='')

def printwait():
    global End
    while not End:
        time.sleep(3)
        print(' ')
        print('waiting respond')


def reqinput():
    global End
    while not End:
        name = input("Input your name:")
        End=name=='@end'

End=False

t1=threading.Thread(target=printwait)
t2=threading.Thread(target=reqinput)

t1.start()
t2.start()

t1.join()
t2.join()

print('done')
Input your name:waiting respond
im trying twaiting respond
o typwaiting respond
e somethingwaiting respond
 here
Input your name:waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
@waiting respond
end
waiting respond
done
当我正在打字,然后睡眠时间到期时,它把事情搞砸了,就像这样:

import threading
import time
import sys

def printf(str, *args):
    print(str % args, end='')

def printwait():
    global End
    while not End:
        time.sleep(3)
        print(' ')
        print('waiting respond')


def reqinput():
    global End
    while not End:
        name = input("Input your name:")
        End=name=='@end'

End=False

t1=threading.Thread(target=printwait)
t2=threading.Thread(target=reqinput)

t1.start()
t2.start()

t1.join()
t2.join()

print('done')
Input your name:waiting respond
im trying twaiting respond
o typwaiting respond
e somethingwaiting respond
 here
Input your name:waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
waiting respond
@waiting respond
end
waiting respond
done

虽然我希望它只是在上面一行打印“等待响应”,或者只是在“等待响应”之后移动我试图键入的内容

你想保持输入和输出分开吗?只使用
stdin
stdout
这将是一件复杂的事情。您需要的是在终端中为输入和输出设置单独的区域。有这样的库可以帮助你。是的,但仍然在同一个控制台窗口上