E
E
Emil Sagatdinov2017-01-11 13:56:14
Python
Emil Sagatdinov, 2017-01-11 13:56:14

How to convert data type to string?

Hello, I am writing a small program - an alarm clock, I thought for a long time about how to implement it, I decided to use the following code (I will leave it below) the essence of the code is to check the current date and time every 15 seconds and compare them with the data entered by the user, but when trying to compare the data type with a string (it is entered by the user), it displays an error (I will also throw it below), how can this data type be converted to a string?
The code:

import winsound
import time
import datetime
function = input("Введите будильник или таймер ")
function = function.lower()
if function in "таймер":
    a = float(input("Введите время(в секундах) через которое должен прозвучать сигнал "))
    time.sleep(a)
    winsound.PlaySound('zvuki-zvonok_budilnika.wav', winsound.SND_FILENAME)
elif function in "будильник":
    c = input("h:m")
    while True:
        b = datetime.datetime.now()
        if b[12:18] in c:
            winsound.PlaySound('zvuki-zvonok_budilnika.wav', winsound.SND_FILENAME)
        else:
            time.sleep(15)

Mistake:
C:\Python32\python.exe D:/Emil/Python/timer/timer.py
Введите будильник или таймер будильник
h:m15:55
Traceback (most recent call last):
  File "D:/Emil/Python/timer/timer.py", line 14, in <module>
    if b[12:18] in c:
TypeError: 'datetime.datetime' object is not subscriptable

Process finished with exit code 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WalletDat, 2017-01-11
@emilka174

in your interpretation it can be like this:

b =  datetime.datetime.now().timetuple()
if str(b[3])+':'+str(b[4]) == c:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question