K
K
Keste ...2018-10-11 21:26:25
Python
Keste ..., 2018-10-11 21:26:25

What is wrong with my code(Alarm Clock)?

import time # Для работы с временем
import re # Для работы с регулярными выражениями
import threading # Модуль для работы с потоками

def thread(my_func):
  def wrapper(*args, **kwargs):
    my_thread = threading.Thread(target=my_func, args=args, kwargs=kwargs)
    my_thread.start()
  return wrapper

z = input("> ")

if 'поставь' in z and 'будильник' in z or 'установи' in z and 'будильник' in z or 'будильник' in z and 'на' in z:
  @thread
  def Alarm(t1, t2):
    while t1 != t2:
      time.sleep(1)
    print("Проснись!!!")
  def time_to(text):
    datex = re.findall(r'\d{2}[.-:]\d{2}',text)
    if datex:
      date = datex[0].replace('-','').replace('.','').replace(':','')
      whattime = date
    return f"{whattime}"
  def what_time():
    time_list = time.localtime()
    hours = time_list[3]
    minut = time_list[4]
    return f"{hours}{minut}"
  print("Будильник установлен!")
  Alarm(time_to(z), what_time())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Dunaev, 2018-10-11
@Keste

It will print "Wake up!!!" if you set the alarm to the current time (i.e. so that it goes off immediately). And all because in each iteration it is necessary to re-learn the current time through what_time(), instead of comparing with a variablet2

def Alarm(t1, t2):
    while t1 != what_time():
      time.sleep(1)  
    print("Проснись!!!")

And one more thing: storing time in strings and comparing them all the time is not the best solution. It is much faster and more convenient to store and compare time as a number.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question