Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在打印到屏幕时获取用户输入_Python_Multithreading_Input_While Loop_Output - Fatal编程技术网

Python 在打印到屏幕时获取用户输入

Python 在打印到屏幕时获取用户输入,python,multithreading,input,while-loop,output,Python,Multithreading,Input,While Loop,Output,我希望在打印到屏幕时获得用户输入。我对此做了很多研究。但是什么也没找到。我对Python中的类编程不是很精通,所以我不理解stackoverflow的其他示例 例如: import time def user_input(): while True: raw_input("say smth: ") def output(): while True: time.sleep(3) print "hi" input() output

我希望在打印到屏幕时获得用户输入。我对此做了很多研究。但是什么也没找到。我对Python中的类编程不是很精通,所以我不理解stackoverflow的其他示例

例如:

import time

def user_input():
    while True:
        raw_input("say smth: ")

def output():
    while True:
        time.sleep(3)
        print "hi"

input()
output()

我希望提示符保持不变,即使输出是打印“hi”。我已经试过一个例子,但是输入提示消失了,即使它在
while
循环中。

首先,让我们看一下您的代码

import time

def user_input():
    while True:
        raw_input("say smth: ")

def output():
    while True:
        time.sleep(3)
        print "hi"

input()
output()
您正在调用的
input()
,实际上它不是您的函数名。这是
user\u input()
。此外,一旦调用了
user\u input()
,将永远不会调用
output()
,因为
user\u input()
中的
while True
条件始终为True,这意味着它永远不会退出函数之外

另外,如果您想使用多线程进行一些操作,但不了解Python中的类,那么您可能应该编写一个与类相关的教程

在一个多线程程序上看一下另一个