I need help with this exercise please. I have to ask the user if want to set an alarm, either for a specific date or for certain days of the week. In addition to printing the time adding 1 and I have to say if it is day or night (This last part of the code was made by the teacher of the course, I did all the alarm).
The code should open a.txt file but it does not, I have run the code several times and checked the Pycharm but nothing. Here it is
from time import sleep
import datetime
NIGHT_STARTS = 19
DAY_STARTS = 8
HOUR_DURATION = 1
def write_file_and_screen(text, file_name):
with open(file_name, "a+") as file_text:
file_text.write("{}{}".format(text, "\n"))
print(text)
def week_date_to_day(day):
days_list = {0: "Monday", 1: "Tuesday", 2: "Wednesday", 3: "Thursday", 4: "Friday", 5: "Saturday",
6: "Sunday"}
day_weekday = day.weekday
if day_weekday == days_list.keys():
week_day_of_the_day = days_list.get(day_weekday)
return week_day_of_the_day
def main():
today = datetime.date.today()
current_time = datetime.datetime.now()
is_night = False
want_alarm = input("Do you want to set a alarm? Yes/No ")
if want_alarm == "Yes":
# Aquí preguntamos si desea una alarma para una fecha específica.
specific_date = input("Do you want an alarm in a specific date? Yes/No ")
if specific_date == "Yes":
date_user = input("Tell me the date. (dd/mm/yyyy) ")
date_format = datetime.datetime.strptime(date_user, "%d/%m/%Y")
if datetime.date.today() == date_format:
write_file_and_screen("ALARM. It's {}".format(date_format), "Specific alarm.txt")
print("ALARM. It's {}".format(date_format))
elif specific_date == "No":
# Aquí preguntamos si desea una alarma normal, haciendo que elija el día y la hora.
normal_alarm = input("Do you want a normal alarm? Yes/No ")
if normal_alarm == "Yes":
hour_alarm = int(input("Hour of the alarm? 0/23 "))
datetime.time(hour=hour_alarm)
days_of_alarm_input = ""
days_of_alarm_list = []
print("Write End to end the loop ")
while not days_of_alarm_input == "End":
days_of_alarm_input = input("Tell me the days that you want to set the alarm 0 to 6, 0 is "
"Monday ""and 6 is Sunday ")
days_of_alarm_list.append(days_of_alarm_input)
if days_of_alarm_input == "End":
for i in days_of_alarm_list:
if today.weekday() == days_of_alarm_list:
write_file_and_screen("ALARM. It's {}".format(week_date_to_day(today)), "Weekdays "
"alarm.txt")
while True: # Se imprime la hora actual y se le va sumando una más, además de que si indica si es de día
# o de noche
sleep(HOUR_DURATION)
current_time += datetime.timedelta(hours=1)
light_changed = False
if (current_time.hour >= NIGHT_STARTS or current_time.hour <= DAY_STARTS) and not is_night:
is_night = True
light_changed = True
elif (DAY_STARTS < current_time.hour < NIGHT_STARTS) and is_night:
is_night = False
light_changed = True
if light_changed:
if is_night:
write_file_and_screen("It's night", "hours.txt")
else:
write_file_and_screen("It's day", "hours.txt")
write_file_and_screen("The hour is {}".format(current_time), "horas.txt")
sleep(2)
if __name__ == "__main__":
main()
When I run the program and I enter the necessary data for the alarm, simply the program goes to the 3rd part of the code and starts printing the time and adding 1 without opening the .txt
file:
The hour is 2019-11-09 19:50:51.614472
The hour is 2019-11-09 20:50:51.614472
The hour is 2019-11-09 21:50:51.614472
The hour is 2019-11-09 22:50:51.614472
The hour is 2019-11-09 23:50:51.614472
The hour is 2019-11-10 00:50:51.614472
The hour is 2019-11-10 01:50:51.614472
The hour is 2019-11-10 02:50:51.614472
The hour is 2019-11-10 03:50:51.614472
The hour is 2019-11-10 04:50:51.614472
The hour is 2019-11-10 05:50:51.614472
If you need to know something else or If I did not explain myself well please tell me. (Sorry for my English)
Update:
The specific date alarm works! Thanks!
but there is another problem. The "normal alarm" does not. When the program is on the line for i in days_of_alarm_list:
it skip it to while True
i = 6, it suppose to pass the for i in days_of_alarm_list: But the program passes to while True
There was a type mismatch earlier, that's why you were going to the third part every time.
datetime.datetime.strptime('10/11/2019',"%d/%m/%Y")
returns 'datetime.datetime(2019, 11, 10, 0, 0)'
datetime.date.today()
returns 'datetime.date(2019, 11, 10)'
replace your if
condition with the following line:
if datetime.date.today() == date_format.date() :
Your code working fine, I tested it on my machine,
In your code you'r sleeping your condition for 1 hour every time, that's why your code taking more time for execute file creation, modification. Exactly waiting for 1 Hour every time when while condition looping
sleep method is
sleep(HOUR_DURATION)
You can check it by changing the time duration from sleep(HOUR_DURATION)
to sleep(5)
Your condition will sleep just for five milliseconds
Happy codding
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.