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:
def valid_year(year):
if year and year.isdigit():
if int(year) >=1900 and int(year) <=2020:
return year
def valid_year_uda(year):
if year and year.isdigit():
year = int(year)
if year >=1900 and year <=2020:
return year
print valid_year('1970') == valid_year_uda('1970')
print type(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 year and year.isdigit():
if int(year) >=1900 and int(year) <=2020:
return int(year) #return an integer
print valid_year('1970')
Post a Comment for "Valid Year Function In Python"