4

Sorry didn't find any other fix, so I created a new question. I'm trying to run a code that prints to the screen the current hour, minute and second. The piece of code I'm using to do this is:

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)

I get the following error when this piece of code tries to execute:

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() and day() are some other functions the program went through before going through time(). If I try executing time() directly, it works! So, here's the entire code from the start till the end of the time() function:

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

It might be something in start(), questions() or day() causing the error. Any ideas? Thanks a bunch!

MarianD
  • 13,096
  • 12
  • 42
  • 54
teolicht
  • 245
  • 2
  • 6
  • 13

3 Answers3

6

In your command

nowTime = datetime.now()

datetime is the module which has no method now().

You probably wanted

nowTime = datetime.datetime.now()

where the first datetime is the module, and the second one is the class in it - with the classmethod now() which creates an object with current local date and time.

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • Read the exception carefully. The `datetime` module _is not_ his problem. He _is_ using `datetime.datetime` by doing `from datetime import datetime`. As Jonrsharpe already said, he's calling the `time` module inside of his function also named `time`. – Christian Dean Jul 05 '17 at 21:04
  • @Christian - You are partially right - my answer fixes just the OP `time()` function. Importing the `time` module inside its `time()` function is definitely a bad idea - but *without* bad consequences for executing it as the `import` statement imports names into *its own* name space. – MarianD Jul 05 '17 at 21:18
  • Renaming `time()` to `my_time()` and changing `datetime.now()` to `datetime.datetime()` fixed it now. Thank you all! – teolicht Jul 05 '17 at 21:29
6

It worked for me, try to not create your own time() method, I renamed it to "my_time()".

The time module defines a lot of functions so, or you just "import time" or you need to specify each function you wanna import like "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()
Bruno Romano
  • 303
  • 2
  • 10
  • Thanks! Renaming `my_time()` and changing `datetime.now()` to `datetime.datetime.now` fixed the problem. Thanks again! – teolicht Jul 05 '17 at 21:31
5

Replace your import time to from time import time.

MarianD
  • 13,096
  • 12
  • 42
  • 54
Salmaan P
  • 817
  • 1
  • 12
  • 32