Python 我输入了时间,但是时间。sleep()仍然赢了';行不通

Python 我输入了时间,但是时间。sleep()仍然赢了';行不通,python,Python,当我运行代码时,我得到: def start(): import time time.sleep(1) start1() def start1(): print ("This is the spy game.") time.sleep(1) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“”,第7行,在开始处 文件“”,第11行,在start1中 NameError:未定义名称“时间” 如何更正此问题?您需要在全球级别导入时间,在您的职能

当我运行代码时,我得到:

def start():
    import time
    time.sleep(1)
    start1()

def start1():
    print ("This is the spy game.")
    time.sleep(1)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第7行,在开始处
文件“”,第11行,在start1中
NameError:未定义名称“时间”

如何更正此问题?

您需要在全球级别导入
时间
,在您的职能范围之外:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 7, in start
  File "<string>", line 11, in start1
NameError: name 'time' is not defined

通过在
start()
函数中导入它,您将其设置为本地名称,
start1
无法使用它。您还必须在该函数中再次导入
time
。最好将其作为全局导入。

最简单的示例-删除所有不相关的内容:
打印
输入
,回答。。。
import time

def start():
    # ...

def start1():
    # ...