Skip to content

Instantly share code, notes, and snippets.

Created August 13, 2012 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3341725 to your computer and use it in GitHub Desktop.
Save anonymous/3341725 to your computer and use it in GitHub Desktop.
Day of Week
import easygui as eg
import random
import time
import sys
def get_date(difficulty):
if difficulty == 'beginner': year = 2012
elif difficulty == 'easy': year = random.randint(2000,2099)
elif difficulty == 'medium': year = random.randint(1900,2099)
elif difficulty == 'hard': year = random.randint(1583,9999)
month = random.randint(1,12)
if month in (9,4,6,11): day = random.randint(1,30)
elif month == 2:
if year%4==0:
if str(year)[:-2] == '00' and year % 400 ==0:
day = random.randint(1,29)
elif str(year)[:-2] != '00':
day = random.randint(1,29)
else: day = random.randint(1,28)
else: day = random.randint(1,31)
return day,month,year
def calculate_day(day,month,year):
#get century day
century = int(str(year)[0:2] +'00')
a = -400
while a <= 9900:
if century == a:
centuryDay = 2 #tuesday
break
a = a+400
a = -300
while a <= 9900:
if century == a:
centuryDay = 0 #sunday
break
a = a+400
a = -200
while a <= 9900:
if century == a:
centuryDay = 5 #friday
break
a = a+400
a = -100
while a <= 9900:
if century == a:
centuryDay = 3 #wednesday
break
a = a+400
#get leapyear
if year%4==0 and str(year)[:-2]!='00': leapyear = True
elif year%4==0 and str(year)[:-2]=='00':
if year%400 == 0: leapyear = True
else: leapyear = False
elif year%4 != 0: leapyear = False
#get difference
if month == 1:
if leapyear == True:
difference = day - 4
else: difference = day - 3
elif month ==2:
if leapyear == True:
difference = day - 29
else: difference = day - 28
elif month ==3: difference = day
elif month ==4: difference = day - 4
elif month ==5: difference = day - 9
elif month ==6: difference = day - 6
elif month ==7: difference = day - 11
elif month ==8: difference = day - 8
elif month ==9: difference = day - 5
elif month ==10: difference = day - 10
elif month ==11: difference = day - 7
elif month ==12: difference = day - 12
#get dozens, remainder, and fours
shortYear = int(str(year)[-2:])
dozens = shortYear/12
remainder = shortYear - (dozens*12)
fours = remainder/4
#return day of week
dayOfWeek = (difference+centuryDay+dozens+remainder+fours)
while dayOfWeek >=7: dayOfWeek = dayOfWeek - 7
while dayOfWeek < 0: dayOfWeek = dayOfWeek + 7
if dayOfWeek == 0: return 'Sunday'
elif dayOfWeek == 1: return 'Monday'
elif dayOfWeek == 2: return 'Tuesday'
elif dayOfWeek == 3: return 'Wednesday'
elif dayOfWeek == 4: return 'Thursday'
elif dayOfWeek == 5: return 'Friday'
elif dayOfWeek == 6: return 'Saturday'
highscore = 99999999999
difficulty = eg.buttonbox(msg='Choose the difficulty level', title='Guess the date!', choices=('Beginner','Easy','Medium','Hard'), image=None, root=None)
difficulty = difficulty.lower()
while True:
day,month,year = get_date(difficulty)
months = 'January February March April May June July August September October November December'.split()
date = months[month-1]+' '+str(day)+', '+str(year)
days = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'.split()
startTime = time.clock()
selection = eg.buttonbox(msg='The date is '+str(date), title='Guess the day!', choices=days, image=None, root=None)
dayOfWeek = calculate_day(day,month,year)
if selection == dayOfWeek:
score = round(time.clock()-startTime,1)
if score < highscore: highscore = score
if eg.ccbox(msg='You are correct! You solved it in '+str(score)+' seconds. Your highscore is '+str(highscore)+' seconds.', title=' ', choices=('Continue', 'Quit'), image=None):
pass
else: sys.exit()
else:
if eg.ccbox(msg='The answer was '+str(dayOfWeek), title=' ', choices=('Continue', 'Quit'), image=None):
pass
else: sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment