输入到提醒程序的所有时间以秒计

2024-05-28 22:57:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,当运行这个程序时,不管发生什么,你输入的每一件东西都被计算为秒,而不是你实际选择的单位。你知道吗

        __author__ = 'Exanimem'
# Homework Notifier Version 0.1.5 It works a bit better. Kind of.

import time
import threading
import webbrowser
import winsound
import ctypes
import sys
import math
import pyglet

# TO-DO
# NOTE: NOT LISTED IN ORDER OF PRIORITY
# Add months, years, decades, and centuries including system to detect what month, year, decade, and centry it is
# Add ability to remind at a specific time in a unit, like "4:50 in 1 day"
# Detect spelt out numbers as numbers
# Have that you press enter then answer
# Have message box be brought to front of the screen
# Have notifications still come when application closed
# Combine unit and digit function
# User Friendly UI?
# Allow users to input time like "4:30 PM EST"
# Autodetect timezone
# Recorded log to look back on past notifications?
# Configurable beep (with music)
# Restart function (Instead of stopping program at whatever point, have option to create new notification)
# Multiple notifications
# Test stop function further and improve
# Save notification's from last time opened

# KNOWN BUGS
# Everything counted as seconds
# Occasionally message box will not appear

HW = input("What homework should I remind you to do?")
# Enter your homework
remind = input("When would you like me to remind you of this?")
# Enter desired time

remind = float(remind)

unit = input("Will your unit be in seconds, minutes, hours, days, or weeks?")
# Enter correct unit

if unit == "seconds":
    remind*1

    if unit == "minutes":
        remind * 60

    if unit == "hours":
        remind * 3600

    if unit == "days":
        remind * 86400

    if unit == "weeks":
        remind * 604800

continuous = input("Would you like to have the notification be continuous?")

print(
    "You may now leave the application in the background. Closing the application and shutting down your computer will deactivate the notification you have planned.")

while continuous == "yes":

    time.sleep(remind)

    Freq = 2500  # Set Frequency To 2500 Hertz
    Dur = 1000  # Set Duration To 1000 ms == 1 second
    winsound.Beep(Freq, Dur)

    print("The message box has opened, but as another reminder your homework is")

    print(HW)

    ctypes.windll.user32.MessageBoxW(0, HW, "Homework!!!", 1)

    if input("To stop the loop and close the program, please type in 'stop'") == "stop":
        break

if continuous == "no":
    time.sleep(remind)

    Freq = 2500  # Set Frequency To 2500 Hertz
    Dur = 1000  # Set Duration To 1000 ms == 1 second
    winsound.Beep(Freq, Dur)

    print("The message box has opened, but as another reminder your homework is")

    print(HW)

    ctypes.windll.user32.MessageBoxW(0, HW, "Homework!!!", 1)

我最初以为问题是第一个if上的缩进,但如果它是有意的,程序就停止工作了。我想弄清楚这一点已经有一段时间了,但我一辈子都搞不懂。救命啊?你知道吗


Tags: andthetoinimportyouinputyour
2条回答

您应该使用您计算的

即使您正在进行正确的计算,也永远不会更新remind的值,这意味着您正在有效地计算一些东西,然后将其丢弃。你知道吗

示例

remind *  3600 # will simply calculate and discard the value
remind *= 3600 # remind = remind * 3600

代码很难理解!

if unit == "seconds"之后的if的缩进级别看起来只有当unit等于"seconds"时才会对它们进行计算。如果代码中的空白实际上是编写的,这样解释程序就不会以这种方式读取代码,那么这可能不是问题,但它看起来很奇怪,而且非常容易出错。你知道吗

示例

if unit == "seconds":
    remind*1

    if unit == "minutes": # this will only execute if "unit == "seconds"
        remind * 60
if unit == "seconds":
  remind *= 1

if unit == "minutes":
  remind *= 60

如何解决问题

在当前执行“计算并丢弃”舞蹈的每一点上,更新代码,以便实际存储计算值,以便将来使用。你知道吗

还要修复缩进级别,使其看起来不再像是使用嵌套的if条件。你知道吗

if unit == "seconds":
  remind *= 1 # useless

if unit == "minutes":
  remind *= 60

if unit == "hours":
  remind *= 3600

if unit == "days":
  remind *= 86400

if unit == "weeks":
  remind *= 604800

Note: Another point worth raising is that unit could never match more than one of those if-statements, you are better of using if-elif-statements. More information about if-statements can be found here

如前所述,您实际上并没有更新remind,您的if不应该缩进到第一个中,但是一种更简单的方法是使用dict映射秒、小时等。。到适当的值:

mapping = {"seconds":60,"hours":3600,"days":86400,"weeks":604800}
unit = input("Will your unit be in seconds, minutes, hours, days, or weeks?")

# do lookup on mapping and increment remind
remind *= mapping.get(unit,1)

if语句的所有逻辑组合在remind *= mapping.get(unit,1)中,如果用户输入了无效的内容,它将从dict中提取适当的值,或者返回1。你知道吗

您可能希望实际使用while循环并验证用户是否输入了一些有效的输入,例如。你知道吗

mapping = {"seconds":60,"hours":3600,"days":86400,"weeks":604800}
while True:
    unit = input("Will your unit be in seconds, minutes, hours, days, or weeks?")
    if unit in mapping:     
       remind *= mapping[unit]
       break
    print("Invalid option")

如果您使用的是If逻辑,那么就使用if/elif,一个单位不能同时是五个不同的事物,If总是被计算,但是elif只有在前面的If或elif被计算为False时才被计算:

if unit == "seconds":
  remind *= 1 # useless

elif unit == "minutes":
  remind *= 60

elif unit == "hours":
  remind *= 3600

elif unit == "days":
  remind *= 86400

elif unit == "weeks":
  remind *= 604800

但是,当用户没有输入有效的输入时,这种逻辑同样无法处理。你知道吗

相关问题 更多 >

    热门问题