Skip to content

Instantly share code, notes, and snippets.

@3lpsy
Created January 19, 2018 02:30
Show Gist options
  • Save 3lpsy/d0accd6de4818842e8108739032b958e to your computer and use it in GitHub Desktop.
Save 3lpsy/d0accd6de4818842e8108739032b958e to your computer and use it in GitHub Desktop.
A sample answer to lab question
import os
import datetime
import sys
NOW = datetime.datetime.now()
NOW_YEAR = NOW.year
def is_valid_age(age):
return age > 0 and age < 100
def ask_age():
age_question = "How Old Are You? "
return input(age_question)
def check_age():
age = 0
while not is_valid_age(age):
try:
age = int(ask_age())
except ValueError as e:
print("Invalid Age")
age = 0
return age
def ask_future():
future_question = "In what future year would you like me to tell you your age? "
return input(future_question)
def is_valid_future(future):
return isinstance(future, int) and future > NOW_YEAR
def calculate_future(age, future):
return (future - NOW_YEAR) + age
def check_future(age):
future = 0
while not is_valid_future(future):
future = int(ask_future())
future_age = calculate_future(age, future)
return future_age, future
def run():
age = check_age()
future_age, future = check_future(age)
msg = "You will be {future_age} in {future}".format(future_age=future_age, future=future)
print(msg)
if __name__ == '__main__':
sys.exit(run())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment