1

I've been trying to solve this simple puzzle for hours, I'm new to python and the idea with this function is to be able to validate that the input corresponds to a numerical value and that otherwise restart the system, I can't get the function to return the value as it should and That is why I ask for your collaboration, without further ado, thank you very much.

import json
def cantidad_alumnos_cargar():
  try:
    a = int(input("Cantidad de alumnos a cargar: "))
    return a
  except:
    print("pone numeros!")
    cantidad_alumnos_cargar()

print(cantidad_alumnos_cargar())
Lei Yang
  • 3,970
  • 6
  • 38
  • 59
  • similiar to [asking-the-user-for-input-until-they-give-a-valid-response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Lei Yang Apr 13 '21 at 00:49
  • If i int first por example and "a", then return "none" Cantidad de alumnos a cargar: a pone numeros! Cantidad de alumnos a cargar: 2 None – santiago arriola Apr 13 '21 at 00:54

1 Answers1

0
import json
def cantidad_alumnos_cargar():
    try:
        a = input(int("Cantidad de alumnos a cargar: "))
        return a
    except:
        print("pone numeros!")
        cantidad_alumnos_cargar()
    
    print(cantidad_alumnos_cargar())

You should be calling input() then int(), not int() then input().

Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
millah
  • 3
  • 2
  • Did you try running this code? Note how you are trying to pass a string which is not of an integer. This would immediately raise a `ValueError`, since any string passed to `int()` must be that of a base-10 integer. – Jacob Lee Apr 13 '21 at 02:44
  • recursive makes no sense here. – Lei Yang Apr 23 '21 at 05:47