Skip to content Skip to sidebar Skip to footer

Valid Year Function In Python

Here is the udacity.com web dev course, they ask to write a program for valid year, anything between 1900 and 2020 is a valid year...Now when I submit my below code it gives this e

Solution 1:

You need to return an int, because the udacity's function returns an integer:

defvalid_year(year):
  if year and year.isdigit():
    ifint(year) >=1900andint(year) <=2020:
      return year

defvalid_year_uda(year):
  if year and year.isdigit():
    year = int(year)
    if year >=1900and year <=2020:
      return year

print valid_year('1970') == valid_year_uda('1970')
printtype(valid_year('1970')), type(valid_year_uda('1970'))

output:

False
<type'str'> <type'int'>

This can be fixed easily just replace return year with return int(year):

def valid_year(year):
  if yearand year.isdigit():
    if int(year) >=1900andint(year) <=2020:
      returnint(year)  #return an integer

print valid_year('1970')

Post a Comment for "Valid Year Function In Python"