Error invalid literal for int () with base 10

2

Hello friends, I'm creating a program in Python and it gives me that error. Here is my code to know if you can help me, thank you very much

def calculoMediana(list):
  mediana = float(0)
  tamaño = len(list)
  if tamaño %2 ==0:
    mediana = (list[(tamaño // 2)] + list[(tamaño //2)-1])/2
    return mediana
  else:
    mediana = list[(tamaño//2)]
    return mediana

def lista():
  continuar = "SI"
  while continuar.upper() == "SI":
    n = int(input("Ingrese el tamaño del que desea su lista:"))
    cantidad = 0
    list = []
    while cantidad<n:
      valor = int(input("Favor ingrese un valor de la lista:"))
      cantidad = cantidad + 1
      list.append(valor)
    list.sort()
    print (calculoMediana(list))
    continuar = input("Desea continuar (SI/NO)?")

try:
  lista()


except ValueError:
  print("Favor ingrese un numero entero")
  pass
  lista()

my mistake says

  

valueerror invalid literal for int () with base 10: 'm'

and then I do not care, but

  

valueerror invalid literal for int () with base 10: 'p'

Thanks for your help

    
asked by Elias Arias 03.10.2017 в 03:58
source

1 answer

2

So much in this line

 n = int(input("Ingrese el tamaño del que desea su lista:"))

as in this

valor = int(input("Favor ingrese un valor de la lista:"))

you are waiting for a value of numeric type to assign it to the variable, since it is being converted to an integer by int() , if you enter a non-numeric value, you will get the error:

  

invalid literal for int () with base 10:

Actually you have an exception handling but when you do it you are calling the function lista() , which does not have defined exception handling, as an option you could avoid calling this method and in this way your program would end indicating that you should enter an entire number:

try:
    lista()

except ValueError:
    print("Favor ingrese un numero entero")
    #pass
    #lista()
    
answered by 03.10.2017 в 04:22