Skip to content

Instantly share code, notes, and snippets.

@wkta
Created March 7, 2014 23:44
Show Gist options
  • Save wkta/9422614 to your computer and use it in GitHub Desktop.
Save wkta/9422614 to your computer and use it in GitHub Desktop.
#MonthOfCode day 6 - time
from datetime import timedelta, date, datetime
import random
import time
import os
import string #for the split function
def respPositiveTo( question ):
"""
this function aims at displaying a question
& validating the Y/N answer
"""
valid_answer = False
while(not valid_answer):
print question,'y/n'
answer = raw_input()
if( not ('y'==answer or 'n'==answer)):
print 'answer y or n, please.'
continue
valid_answer=True
return('y'==answer )
def detBirthDate():
print 'on what date were you born?'
print 'please use the format: yyyy,mm,dd'
birthday_stuff = map( int,
string.split( raw_input(), ',' ) )
birthday_d = date( *birthday_stuff )
return birthday_d
def real_time_disp( sec_val, a_date ):
os.system('clear')# replace by os.system('cls') on windows systems
print 'hit Ctrl+C to quit'
print 'approx.',sec_val, 'seconds left to do something meaningful. Estimated date of death:',a_date
smokes = respPositiveTo( 'do you smoke frequently?')
is_male = respPositiveTo( 'is your sex male?')
is_fit = respPositiveTo( 'do you pratice sport every week?')
is_vegan = not respPositiveTo('do you usually eat meat?')
is_married = respPositiveTo('are you married?')
birth_date = detBirthDate()
print 'computing estimated life-span...'
final_age = float( random.randint(80,90) )
if(smokes):
final_age *= 0.8
if(is_male):
final_age *= 0.92
if(is_fit):
final_age *= 1.09
if(is_vegan):
final_age *= 1.07
if(is_married):
final_age *= 1.03
final_age_sec = int( final_age*365.25 )
estimated_death = birth_date + timedelta( days=final_age_sec)
delta_now_death = (estimated_death - datetime.now().date() )
remaining_seconds = delta_now_death.days * 24*3600
real_time_disp(remaining_seconds, estimated_death)
while(True):
time.sleep(1)
remaining_seconds -=1
real_time_disp(remaining_seconds, estimated_death)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment