I'm relatively new at programming in general including Python. But I've tried to create a program in Python that alerts users through a pop up window to perform an action every 2 hours.
import time
import ctypes
count = 0
print "This break message was sent on "+time.ctime()
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextAskingToDoSomething", "WindowTitle", 0x40 | 0x4)
if returnValue == 6:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerYes", "SWindowTitle", 0x40 | 0x0)
elif returnValue == 7:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerNo", "WindowTitle", 0x40 | 0x0)
while (count < 4):
time.sleep(60*60*2)
print "This break message was sent on"+time.ctime()
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextAskingToDoSomething", "WindowTitle", 0x40 | 0x4)
if returnValue == 6:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerYes", "WindowTitle", 0x40 | 0x0)
elif returnValue == 7:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerNo", "WindowTitle", 0x40 | 0x0)
count = count + 1
time.localtime()
It essentially calls a message box with a title and a text in it and if the user clicks one button then another one follow up message box appears. If the user clicks on the other button, then another follow up message appears. This process is repeated every 2 hours for 5 times.
How could I improve this code to be shortened? Could I use a function somehow? Or what would be a way to shorten it and avoid me repeating myself too much (if that is the case)?
returnValue = messageBox(None, "Text", "Title", 0x40 | 0x0)
inside of thoseif
conditions(inwhile
loop) if you're actually ignoring them when the while loop restarts? \$\endgroup\$ – Ashwini Chaudhary Feb 25 '15 at 3:17returnValue = messageBox(None, "TextRespondingToAnswerNo", "WindowTitle", 0x40 | 0x0)
inside of the while loop, you're not using thesereturnValue
anywhere. So, are thosereturnValue
actually required? Currently you're re-calculatingreturnValue = messageBox(None, "TextAskingToDoSomething", "WindowTitle", 0x40 | 0x4)
every time thewhile
loop re-starts. \$\endgroup\$ – Ashwini Chaudhary Feb 25 '15 at 4:27