所有时间都被计入提醒程序计为秒

时间:2015-10-05 21:08:23

标签: python python-3.x

我遇到的问题是,在运行此程序时,无论如何,您输入的所有内容都被计为秒,而不是您实际选择的wahtever单位。

        __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上的缩进,但如果它是打算的话,程序就不再起作用了。我已经尝试了一段时间来解决这个问题,但我不能为我的生活做好准备。帮助

2 个答案:

答案 0 :(得分:1)

您应该使用您计算的内容

即使你正在进行正确的计算,你也永远不会更新remind的值 - 这意味着你正在有效地计算你随后扔掉的东西。

<子> 示例

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

代码令人困惑 - 缩进事项!

if之后if unit == "seconds"的缩进级别看起来只有在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

如何解决问题

在您正在进行“计算并丢弃” -dance的每一点上,更新代码,以便您实际存储计算出的值,使其可供将来使用。

同时修复缩进级别,使其看起来不再使用嵌套的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语句的更多信息可以在here

找到

答案 1 :(得分:0)

正如已经指出的那样,你实际上并没有更新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被评估为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

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