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

Python 输入提醒程序的所有时间均以秒计,python,python-3.x,Python,Python 3.x,我的问题是,当运行这个程序时,无论发生什么,你输入的所有东西都被计算为秒,而不是你实际选择的单位 __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 im

我的问题是,当运行这个程序时,无论发生什么,你输入的所有东西都被计算为秒,而不是你实际选择的单位

        __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上的缩进,但如果它是有意的,那么程序就停止工作了。我已经想了一段时间了,但我一辈子都想不出来。帮助?

您应该使用您计算的内容 即使您进行了正确的计算,您也不会更新
提醒
的值,这意味着您正在有效地计算一些东西,然后将其扔掉

示例


代码是令人困惑的缩进问题!
if unit==“seconds”
之后的
if
s的缩进级别看起来只有在
unit
等于
seconds
时才会对其进行计算。如果代码中的空白实际上是编写的,这样解释程序就不会以这种方式读取代码,那么这可能不是问题,但是它看起来很奇怪,而且很容易出错

示例


如何解决这些问题 在您当前执行“计算并放弃”舞蹈的每一点上,更新代码,以便您实际存储计算值,以便将来使用

还要修复缩进级别,使其看起来不再像是在使用嵌套的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
注意:另一点值得一提的是,
unit
不能匹配多个if语句,最好使用if-elif语句。关于if语句的更多信息可以找到


如前所述,您实际上并没有更新
提醒
,您的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总是被计算,但只有当先前的If或elif计算为False时,elif才被计算:

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

但是,当用户输入无效输入时,该逻辑同样无法处理。

您有两个问题。第一,在你乘以任何东西之后,你都不会给它赋值。你的缩进是错误的,而不是提醒*X,我想你是在寻找提醒*=席指定提醒“时间。睡觉(提醒)”,我的缩进的哪一部分是错误的?没有意识到你还在添加它。让我检查一下是否有效。目前为止有效。只是为了确保我能再测试一下,谢谢!我只是再次测试一切以确保它正常工作。非常感谢您提供的有关其工作原理的所有信息!如果我在if/elif代码的末尾放一个else语句来表示是否存在无效输入,这也行吗?@Zombiex100,确实行,但如果没有循环回到起始处,您将无法返回代码以再次请求输入,您可以在一段时间内放入if,如我的答案中所示,如果输入有效,则递增并中断,或者打印消息并再次询问。dict映射将为您提供与使用一组if/elif完全相同的逻辑,只是一种更简洁的方法。我有
而True:if unit==“seconds”:提醒*=1 break elif unit==“minutes”:提醒*=60中断elif单位==“小时”:提醒*=3600中断elif单位==“天”:提醒*=86400中断elif单位==“周”:提醒*=604800中断其他:打印(“无效单位”)
但它只是重复“无效单位”与其说是代码本身,不如说是代码本身。非常感谢。
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
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)
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 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