Skip to content

Instantly share code, notes, and snippets.

@akshaynawale
Created October 8, 2020 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshaynawale/097e373da6236acdb9b8e3be501a2353 to your computer and use it in GitHub Desktop.
Save akshaynawale/097e373da6236acdb9b8e3be501a2353 to your computer and use it in GitHub Desktop.
Character input solution for practicepython.org exercise 1
import sys
from datetime import date
# taking persons name
name: str = input("Please enter your name: ")
age_str: str = input("Please enter your age in whole number (e.g. 28): ")
# validate age and convert it into integer
# removing the leading and traliing spaces
age_str = age_str.strip()
# try to convert the string into int
try:
age: int = int(age_str)
except ValueError:
print(f"unable to convert string {age_str} to integer value please provide valid integer age value")
sys.exit(1)
except:
print(f"unknown error occured when trying to convert age string {age_str} to integer")
sys.exit(1)
year = date.today().year
life_remaining = 100 - age
year = year + life_remaining
print(f"Name: {name}\nAge: {age}\nYou will be 100 years old in year: {year}\nDont Waste Your Time")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment