python - 类型错误 : 'module' object is not callable - using datetime

标签 python python-3.x

<分区>

抱歉没有找到任何其他修复方法,所以我创建了一个新问题。 我正在尝试运行将当前小时、分钟和秒打印到屏幕上的代码。我用来执行此操作的代码是:

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() 中的某些内容导致了错误。有任何想法吗?非常感谢!

最佳答案

它对我有用,尽量不要创建自己的 time() 方法,我将它重命名为“my_time()”。

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()

关于python - 类型错误 : 'module' object is not callable - using datetime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44935376/

相关文章:

python - 如何通过 python click.version_option 装饰器的 Travis CI 测试?

python - 无法使用 Pafy/youtube_dl 初始化新 URL,我收到 "ERROR: This video is unavailable"

python - 如何在不同的终端中 fork python 脚本

python - 属性错误 : 'NoneType' object has no attribute 'delete'

python - 如何检查嵌套列表树的所有元素是否相同?

python - 为什么每次 PyQt5 项目都会收到警告 "QStandardPaths: XDG_RUNTIME_DIR not set"

python - python 列表的子集基于同一列表的元素组,pythonically

python - 合并两个文件中的行(如果它们具有相同的列值)

python - 使用 Flask 和 WTForms 链接表单时出现问题

python - 在 Jupyter 中, '/home/jovyan' 在哪里?