Skip to main content
 首页 » 编程设计

python之所有输入提醒程序的时间都以秒计

2025年05月04日11think

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

        __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 的缩进,但如果它是有意的,程序就会停止工作。我已经尝试解决这个问题一段时间了,但我一辈子都做不到。帮忙?

请您参考如下方法:

你应该使用你所计算的

即使您进行了正确的计算,您也永远不会更新 remind 的值 — 这意味着您正在有效地计算一些您随后丢弃的东西。

示例

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

代码令人困惑——缩进很重要!

if unit == "seconds" 之后的 if 的缩进级别看起来只有当 unit 相等时它们才会被评估到 “秒”。如果您的代码中的空格实际上是为了让解释器不会以这种方式读取您的代码,那么这可能不是问题,但是它看起来很奇怪并且很容易出错。

示例

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