1

I am getting AttributeError: 'str' object has no attribute 'sleep' as specified in the title of this question and I cannot figure out why it is throwing that error message.

Countdown Timer.py

import time, datetime

Year = 2020
Month = 12
Day = 24
Hour = 23
Minute = 18
Second = 50

while True:
    Datetime = datetime.datetime(Year, Month, Day, Hour, Minute, Second)
    diff = Datetime - datetime.datetime.now()
    diff = str(diff)

    days, not_useful, time = diff.split()

    Day1 = days + " " + "Day" # Day

    print(Day1)

    time.sleep(1)
1
  • 1
    try adding print(time) in line 2 and line 14. recognizing any difference ;-) ? Commented Dec 29, 2015 at 11:21

3 Answers 3

11

That's because you locally erased the variable time that contained the module with a string. Here is a correct code:

import time, datetime

Year = 2020
Month = 12
Day = 24
Hour = 23
Minute = 18
Second = 50

while True:
    Datetime = datetime.datetime(Year, Month, Day, Hour, Minute, Second)
    diff = Datetime - datetime.datetime.now()
    diff = str(diff)

    days, not_useful, time_str = diff.split()

    Day1 = days + " " + "Day" # Day

    print(Day1)

    time.sleep(1)
Sign up to request clarification or add additional context in comments.

Comments

1
days, not_useful, time = diff.split()

here you will have 'time' as string. change verb name...

Comments

0

It's because you are using Time as a variable in your code:

time = diff.split()

and the above line is locally overwriting the variable timein the time-module.

Try using a different variable:

time_1 = diff.split()

1 Comment

You said the same thing that was said as the accepted answer. That's the reason I downvoted your answer. I am well aware that you are a new contributor to the site, which is great! Just for future reference make sure that you just don't copy what's said in an answer already (especially when it's an accepted one) unless you can build upon something that someone already said. Happy answering :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.