Question: In the following code, how can we make the int() function inside a try/except Value Error block. For example when it prompts for birth year
In the following code, how can we make the int() function inside a try/except Value Error block.
For example when it prompts for birth year and a user enters some character catch the exception and re-prompt to enter the year. Same with month and day.
And if age is less than 18 or greater than 120 print" too young" if age is valid print " Good to go "
import datetime ageCriteriaSatisfied = False while not ageCriteriaSatisfied: Year = int(input(" Please enter the year you were born: ")) Month = int(input(" Please enter the number of the month you were born: ")) Day = int(input(" Please enter the day you were born: ")) DOB = datetime.datetime(Year, Month, Day) Age = (datetime.datetime.now() - DOB) convertdays = int(Age.days) AgeYears = convertdays / 365 # Rounded to the nearest integer YearsAge = round(AgeYears) if 18 < AgeYears < 120: ageCriteriaSatisfied = True else: print("You should be at least 18 years old and less than 120 years old!") print("You are " + str(YearsAge) + " years old !")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
