Alberto Martínez
Alberto Martínez

Reputation: 251

How to get the index of a number in input in Python

The user needs to put a number and the code gets five digits, how do I make to only take four digits if the user only puts four?

Here it is:

numero = int(input("por favor ingrese un numero: "))

digito1 = numero // 10000
digito2 = (numero//1000) % 10
digito3 = (numero//100) % 10
digito4 = (numero//10) % 10
digito5 = numero % 10

if digito5 is None and digito1 == digito4 and digito2==digito3:
   print ("Es capicua")

if digito1 == digito5 and digito2 == digito4:
   print ("Es capicua")
else:
   print ("No es capicua")

Upvotes: 0

Views: 440

Answers (2)

Michael H.
Michael H.

Reputation: 3483

It looks like you are trying to check if numero is a palindrome. That can be done in a simpler:

numero = int(float(input("por favor ingrese un numero: ")))
if str(numero)==str(numero)[::-1]: 
    print("Es capicua")
else: 
    print("No es capicua")

Upvotes: 1

brb
brb

Reputation: 1179

I don't understand 'capicua' so I am not 100% sure what you are trying to do, but it looks like you want to print 'Es capicua' if an integer is symmetrical around the middle (eg if you have 1221 or 12321) and 'No es capicua' if it is not symmetrical around the middle (eg 1234 or 12345).

The below will do this, and is generalised for different length integers.

numero = int(float(input("por favor ingrese un numero: ")))

digitos = list(str(numero))

symmetricalAroundMedian = True
for i in range(0,int(len(digitos)/2)):
    if digitos[i] != digitos[len(digitos)-1-i]:
        symmetricalAroundMedian = False

if symmetricalAroundMedian:
    print ("Es capicua")
else:
    print ("No es capicua")

Upvotes: 0

Related Questions