I was writting this function on Python 2.7, it should return the letter which correspond to any given number n. The function is working for every letter, except for the letter 'o', i.e., when i put n=15, 41,... etc, it returns 'n' instead of 'o'
def chr2(n):
capital='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lower=capital.lower()
indice=(((n/26.0)-(n/26))*26)
return lower[int(indice)-1]
print chr2(15)
The funny thing is that if i print the indice:
def chr2(n):
capital='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lower=capital.lower()
indice=(((n/26.0)-(n/26))*26)
print indice
return lower[int(indice)-1]
print chr2(15)
The result is this:
15.0 n
But if i print the int(indice):
def chr2(n):
capital='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lower=capital.lower()
indice=(((n/26.0)-(n/26))*26)
print int(indice)
return lower[int(indice)-1]
print chr2(15)
The result is this!
14 n
What the hell is going on!? why the int(15.0) is 14??! Because if i just type int(15.0) at the command line obviously the answer is 15, but why in the function the int(15) is 14?? I know that i can change the function or whatever, but i will not sleep until i understand what is going on! Does anyone has an explanation for this?