mardi 24 février 2015

How can I improve this code so I do not repeat myself too much?

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. The code is below:



import time
import ctypes

count = 0

print "This break message was sent on "+time.ctime()
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "Text1", "Title", 0x40 | 0x4)
if returnValue == 6:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "Text", "Title", 0x40 | 0x0)
elif returnValue == 7:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "Text", "Title", 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, "Text", "Title", 0x40 | 0x4)
if returnValue == 6:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "Text", "Title", 0x40 | 0x0)
elif returnValue == 7:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "Text", "Title", 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)?


Aucun commentaire:

Enregistrer un commentaire