mardi 7 janvier 2020

if function doesnt return anything in python

this code:

def century(year):
year = str(year)
if len(year) == 4: # check if year is 4 digits
    if year[2:] == '00': # if year ends with 00
        year = (year[:2]) # return first 2 numbers out of year
        return year
    else:
        year = (int(year[2:]) + 1) # if year doesnt end with 00 add 1 to the first 2 digits
        return year
elif len(year) == 3: # check if year is 3 digits 
    if year[2:] == '00':
        year = (year[1:]) # if year ends with 00 return first digit
        return year
    else:
        year = (int(year[1:]) + 1) # return first digit plus 1 
        return year
else:
    year = 1 # return 1 if year is less than 2 digits
    return year


century(1601)
century(58)
century(356)

this method should return a the century based on what year its given.

doesnt return anything for some reason )=

im running this throw python3 btw

thanks!

Aucun commentaire:

Enregistrer un commentaire