Created
October 21, 2018 14:34
-
-
Save caioariede/1f642d272611c2cd450577c193b674a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import calendar | |
import datetime | |
try: | |
input = raw_input | |
except NameError: | |
pass | |
def random_date(start_year, end_year): | |
"""Generate a random date between `start` and `end`""" | |
# Based on https://stackoverflow.com/a/8170651 | |
start = datetime.datetime(start_year, 1, 1) | |
end = datetime.datetime(end_year, 12, 31, 23, 59, 59) | |
return (start + datetime.timedelta( | |
# Get a random amount of seconds between `start` and `end` | |
seconds=random.randint(0, int((end - start).total_seconds())), | |
)).date() | |
def get_weekday_name(date): | |
return calendar.day_name[date.weekday()].lower() | |
def check(date, user_input): | |
weekday_name = get_weekday_name(date) | |
return len(user_input) >= 3 and weekday_name.startswith(user_input.lower()) | |
try: | |
guess_date = random_date(1900, 2099) | |
user_input = input(str('[Guess the week day] {}: '.format(guess_date))) | |
if check(guess_date, user_input): | |
print('YEAH!') | |
else: | |
print('WRONG!! correct is: {}, keep training :)' | |
.format(get_weekday_name(guess_date))) | |
except KeyboardInterrupt: | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment