类型错误:'module'对象不可调用 - 使用datetime。

4

抱歉,我没有找到其他解决方法,所以我创建了一个新的问题。 我正在尝试运行一段代码,它会将当前的小时、分钟和秒打印到屏幕上。我使用的代码片段如下:

def time():
    import datetime
    import time
    time.sleep(1)
    nowTime = datetime.now()
    print ('Right now it's %s hours, %s minutes and %s seconds.' % (nowTime.hour,nowTime.minute,nowTime.second))
    time.sleep(5)

当这段代码尝试执行时,我遇到了以下错误:
Traceback (most recent call last):
  File "/Users/Teolicht/Desktop/Python/testy.py", line 219, in <module>
    start()
  File "/Users/Teolicht/Desktop/Python/testy.py", line 18, in start
    questions()
  File "/Users/Teolicht/Desktop/Python/testy.py", line 34, in questions
    day()
  File "/Users/Teolicht/Desktop/Python/testy.py", line 52, in day
    time()
TypeError: 'module' object is not callable

start(), questions()day()是程序在执行time()之前经过的其他一些函数。如果我直接尝试执行time(),它可以工作!因此,这里是从开始到time()函数结束的整个代码:

from datetime import datetime
import time
import random
import sys

def start():
    import time
    name = input('Hi. What is your name? ')
    print ('Nice to meet you, ' + str(name) + '.')
    time.sleep(1)
    print ('How old are you?')
    age = input()
    time.sleep(1)
    print (age + '! Cool.')
    time.sleep(2)
    questions()

def questions():
    import time
    print ('Some things you can ask me:')
    time.sleep(1)
    print ('• What day is today? (qdh)')
    time.sleep(1)
    print ('• What time is it? (qhs)')
    time.sleep(1)
    print ('• I want to play a game! (qjj)')
    time.sleep(1)
    print ('• How many days till my birthday? (qda)')
    time.sleep(1)
    questionsAnswer = input()
    if questionsAnswer == 'qdh':
        day()
    elif questionsAnswer == 'qhs':
        time()
    elif questionsAnswer == 'qjj':
        game()
    else:
        birthday()

def day():
    import time
    time.sleep(1)
    nowDay = datetime.now()
    print ('Today is %s/%s/%s' % (nowDay.day,nowDay.month,nowDay.year))
    time.sleep(2)
    dayAnswer = input('Want to know what time it is? "Y" for yes and "N" for no: ').lower()
    if dayAnswer == 'n':
        questions()
    else:
        time()

def time():
    import time
    time.sleep(1)
    nowTime = datetime.now()
    print ('Right now it's %s hours, %s minutes and %s seconds.' % (nowTime.hour,nowTime.minute,nowTime.second))
    time.sleep(5)
        questions()
       ...

可能是在start()questions()day()中出现了错误。有什么想法吗?非常感谢!


你的 day 函数的最后一行是否想要使用 time.time() - Christian Dean
7
你在一个名为 time 的函数中使用了 import time?难怪你对作用域内的内容感到困惑。 - jonrsharpe
没错,jonrsharpe。 - teolicht
我用来做这件事的代码片段是: - Karl Knechtel
3个回答

6

在你的命令中

nowTime = datetime.now()

datetime 是一个模块,其中没有 now() 方法。

你可能想使用:

nowTime = datetime.datetime.now()

第一个 datetime 是一个 module,而第二个是其中的一个 class。这个类方法 now() 会创建一个带有当前本地日期和时间的 object


仔细阅读异常信息。datetime 模块不是问题所在。他通过 from datetime import datetime 使用了 datetime.datetime。正如 Jonrsharpe 已经说过的那样,他在函数内部也使用了名为 timetime 模块。 - Christian Dean
@Christian - 你说的部分正确 - 我的回答只修复了 OP 的 time() 函数。在其 time() 函数内导入 time 模块绝对是个坏主意 - 但是 没有 坏后果,因为 import 语句将名称导入到 它自己的 名称空间中。 - MarianD
time() 重命名为 my_time(),并将 datetime.now() 更改为 datetime.datetime(),现在已经修复了。谢谢大家! - teolicht

6

对我来说有效,尝试不要创建自己的time()方法,我将其重命名为“my_time()”。

time模块定义了许多函数,因此您可以只“import time”,或者您需要指定每个要导入的函数,例如“from time import sleep”。

from datetime import datetime
from time import time, sleep
import random
import sys

def questions():
    print ('Some things you can ask me:')
    sleep(1)
    print ('• What day is today? (qdh)')
    sleep(1)
    print ('• What time is it? (qhs)')
    sleep(1)
    print ('• I want to play a game! (qjj)')
    sleep(1)
    print ('• How many days till my birthday? (qda)')
    sleep(1)
    questionsAnswer = input()
    if questionsAnswer == 'qdh':
        day()
    elif questionsAnswer == 'qhs':
        my_time()
    elif questionsAnswer == 'qjj':
        my_game()
    else:
        my_birthday()

def day():
    sleep(1)
    nowDay = datetime.now()
    print ('Today is %s/%s/%s' % (nowDay.day,nowDay.month,nowDay.year))
    sleep(2)
    dayAnswer = input('Want to know what time it is? "Y" for yes and "N" for no: ').lower()
    if dayAnswer == 'n':
        questions()
    else:
        my_time()

def my_time():
    sleep(1)
    nowTime = datetime.now()
    print ('Right now it\'s %s hours, %s minutes and %s seconds.' % (nowTime.hour, nowTime.minute, nowTime.second))
    sleep(5)
    questions()

def my_game():
    pass

def my_birthday():
    pass

#def start():
name = input('Hi. What is your name? ')
print ('Nice to meet you, ' + str(name) + '.')
sleep(1)
print ('How old are you?')
age = input()
sleep(1)
print (age + '! Cool.')
sleep(2)
questions()

谢谢!将 my_time() 重命名并将 datetime.now() 更改为 datetime.datetime.now 解决了问题。再次感谢! - teolicht

5

将您的import time替换为from time import time


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接