Python 3.3.3 time.sleep() error [duplicate]

2019-09-30 08:14发布

问题:

This question already has an answer here:

  • Python: “global name 'time' is not defined” 5 answers

I'm getting the following error:

The error:"Traceback (most recent call last):
  File "C:\Users\Jamie\Desktop\Python\ATM.py", line 8, in <module>
    time.sleep(2)
NameError: name 'time' is not defined"

here is my code:

http://pastebin.com/q2v7sdbz

Any ideas towards what may be throwing this?

Best,

Jamie

回答1:

the sleep function comes from the python time module so you to import before you call any of it's methods just put

import time

before you use any time stuff. It's normal to just put all of your imports at the top of your code.

see here for more info on importing:

https://docs.python.org/3/reference/import.html



回答2:

You need to import the time module. Just write import time at the top of your file on its own line to import it.