首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输入到提醒程序中的所有时间计为秒。

输入到提醒程序中的所有时间计为秒。
EN

Stack Overflow用户
提问于 2015-10-05 21:08:23
回答 1查看 82关注 0票数 2

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

代码语言:javascript
复制
        __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上的缩进,但是如果它是有意的,程序就会停止工作。我试着弄清楚这件事已经有一段时间了,但我一辈子都做不到。帮助?

EN

回答 1

Stack Overflow用户

发布于 2015-10-05 21:24:43

正如已经指出的,您实际上并不是在更新remind,您的if's不应该缩进第一个内部,但是对您的总体逻辑的一种更简单的方法是使用dict映射秒、小时等。适当的值:

代码语言:javascript
复制
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循环,并验证用户是否输入了一些有效的输入,类似于。

代码语言:javascript
复制
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时才被计算:

代码语言:javascript
复制
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

但是,当用户没有输入有效的输入时,这个逻辑也不会处理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32957916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档