文本文件无法使用open()打开

Thebigbosslong

我需要这项练习的帮助。我必须询问用户是否要为特定日期或一周中的某些天设置闹钟。除了打印加1的时间外,我还必须说是白天还是晚上(代码的最后一部分是由课程的老师制作的,我做了所有警报)。

该代码应该打开一个.txt文件,但是没有,我已经运行了几次该代码,并检查了Pycharm,但是什么也没有。这里是

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

当我运行该程序并输入警报所需的数据时,只需将该程序转到代码的第3部分,并开始打印时间并在不打开.txt文件的情况下加1

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

如果您需要了解其他信息,或者如果我不能很好地解释自己,请告诉我。(对不起我的英语不好)


更新:

特定日期闹钟有效!谢谢!

但是还有另一个问题。“正常警报”没有。当程序在线时,将for i in days_of_alarm_list:其跳过到while True i = 6,它假定在days_of_alarm_list中传递for i: 但是程序传递给while True

帕尔帕丁

之前有一个类型不匹配,这就是为什么您每次都要转到第三部分。

datetime.datetime.strptime('10/11/2019',"%d/%m/%Y")退货
'datetime.datetime(2019, 11, 10, 0, 0)'
datetime.date.today()退货'datetime.date(2019, 11, 10)'

if以下行替换您的条件:

if datetime.date.today() == date_format.date() :

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章