how to return a 4-digit value (neither more nor less) and return with your figures invested in python

0

Friends help me with this question that they left me in a workshop, I've really tried but it does not give me, I tried this:

def invertida(n):

    """
    problema: recibir un numero de 4 cifras y devolver sus cifras invertidas

    entrada: numero

    salida: numero invertido
    """

    numero = (n[::-1])
    return numero 

invertida(1234)

but that of [::-1] does not give me and I would like the person to enter exactly 4 digits, neither more nor less, but I do not know how: (

    
asked by margarita gutierrez 12.02.2017 в 19:25
source

1 answer

1

I do not understand why you say it does not work for you, with this simple example you should work perfectly validating that the number is 4 digits

input = raw_input('Escriba un número de 4 digitos: ')
if len(input) == 4:
  print(input[::-1])
else:
  print("Error, el número debe ser de 4 digitos")

I leave here a functional example of the previous code

EDIT 1 : I had not seen that you wanted it in a show, but if so, this should help you

def cadenaInvertida(n):

  if len(str(n)) == 4:
    return str(n)[::-1]
  else:
    return False

print(cadenaInvertida(1234))

I leave here a functional example of the previous code

What happens is that the function is reading a number as such and this number can not be applied to invert it, so you must first pass it to a variable of type str

    
answered by 12.02.2017 в 19:42