Please review my program and give some tips . I am a beginner. IT is for those people who sit on computer all day and get unhealthy . This will remind them to do eye exercise , physical exercise and to drink water
import pygame
import time
t = time.ctime(time.time())
# noinspection SpellCheckingInspection
totalbreaks2 = 17
# noinspection SpellCheckingInspection
breakcount2 = 0
# noinspection SpellCheckingInspection
totalbreaks = 16
# noinspection SpellCheckingInspection
breakcount = 0
# noinspection SpellCheckingInspection
totalbreaks1 = 10
# noinspection SpellCheckingInspection
breakcount1 = 0
# noinspection SpellCheckingInspection
def health():
# global keywords
global breakcount2
global breakcount
global breakcount1
global totalbreaks1
global totalbreaks
global totalbreaks2
# water program
if breakcount2 < totalbreaks2:
pygame.mixer.init()
pygame.mixer.music.load("water.mp3.mp3")
pygame.mixer.music.play(-1)
query = input('Did you drink water (type drank if yes)?')
if query == 'drank':
with open('water.txt', 'r+') as o:
o.write(f'You drank water at {t} ')
pygame.mixer.music.pause()
time.sleep(120)
breakcount2 += 1
# eye program
if breakcount < totalbreaks:
pygame.mixer.init()
pygame.mixer.music.load("eyes.mp3.mp3")
pygame.mixer.music.play(-1)
query = input('Did you do eyes exercise ?(type eydone if yes)')
if query == 'eydone':
with open('water.txt', 'r+') as o1:
o1.write(f'You drank water at {t} ')
pygame.mixer.music.pause()
time.sleep(1080)
breakcount += 1
# exercise program
if breakcount1 < totalbreaks1:
pygame.mixer.init()
pygame.mixer.music.load("physical.mp3.mp3")
pygame.mixer.music.play(-1)
query = input('Did you do physical exercise (type exdone if yes) ?:')
if query == 'exdone':
with open('water.txt', 'r+') as o2:
o2.write(f'You drank water at {t} ')
pygame.mixer.music.pause()
time.sleep(2400)
breakcount1 += 1
health()
z = input('Do you want to start the program? (y=yes , n=no):')
if z == 'y':
health()
elif z == 'n':
print('ok')
else:
print('type correct input')
1 Answer 1
Variable naming
Your variables should be named something more telling than breakcount
, breakcount1
and breakcount2
. I suggest breakcount_water
(and _eyes
, _physical
) instead.
Simpler logic
Instead of two variables for each kind of break (6 total), you can do with just one variable for each, starting at the max, and subtract 1
for each break. Then, you're done when you reach zero.
That lets you do the same thing with half as many (3) variables to keep track of.
Don't repeat yourself
pygame.mixer.init()
pygame.mixer.music.load("water.mp3.mp3")
pygame.mixer.music.play(-1)
These three lines are identical each time you play music.
Create a function instead that takes the file name as input, and you can call play_music("water.mp3.mp3")
which then executes these three lines in the function.
def play_music(filename):
#the three lines from above
Stack abuse
You're calling the health()
function from inside itself, which means you never let it exit properly and if you did this a few thousand (maybe more) times, the program would crash due to stack overflow.
Instead, you should use a while
loop that checks for the condition to loop again, to start over without calling the function another time. That way, you can also initialise your variables at the start of the function (before the while-loop) and you don't need any global variables, since the function has access to them inside its own scope.
-
\$\begingroup\$ can i run 3 while loops at same time? \$\endgroup\$ScienceXDream– ScienceXDream2020年08月19日 04:53:25 +00:00Commented Aug 19, 2020 at 4:53
-
\$\begingroup\$ You can with multiprocessing, but you're not doing that now so that's out of scope for the answer I think. \$\endgroup\$user985366– user9853662020年08月19日 10:19:21 +00:00Commented Aug 19, 2020 at 10:19