Question
while 1: try: import datetime Year = int(input( Please enter the year you were born: )) Month = int(input( Please enter the number of the
while 1:
try:
import datetime
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
# round to the nearest integer
YearsAge = round(AgeYears)
print("You are " + str(YearsAge) + " years old !")
if 18
break
# if age is 18 or less and over 120 or more
print("You should be at least 18 years old and less than 120 years old!")
# if there is any exception continue and ask again
except AgeYears:
continue
http://easypythondocs.com/validation.html
use one of the two methods suggested along one or more of the validation techniques provided and fix your code to gracefully and securely handle user input errors.
Just pick a smaller section of the code to fix. Provide the code before and after the fix. Be sure to demonstrate the new fix works as expected by providing screen captures.
Not secure | easypythond... NP + * Easy Python Docs 3.5 Docs Validation in Python Search docs Validation Definition Output Input Comments Variables Strings operations Arithmetic and mathematics Comparison and logical operators Data types and conversions When we accept user input we need to check that it is valid. This checks to see that it is the sort of data we were expecting. There are two different ways we can check whether data is valid. Method 1: Use a flag variable. This will initially be set to false . If we establish that we have the correct input then we set the flag to True . We can now use the flag to determine what we do next (for instance, we might repeat some code, or use the flag in an if statement). For loops If statements While loops Lists (Arrays) Procedures Method 2: Use try/except. Here, we try to run a section of code. If it doesn't work (for instance, we try to convert a string to a number, but it doesn't contain a number) then we run the except block of code. Types of validation: Functions File handling Random numbers e Validation Validation technique Definition Type check Meaning Checking the data type e.g. int, float etc. Checking the length of a string Checking if a number entered is between two numbers Length check Range check Easy example Syntax e Examples Key points SQL Extras Easy example while True: try: age = int(input('How old are you? ')) break except ValueError: print('Please enter whole number') print('Your age is: ' + str(age)) Show/Hide OutputStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started