Skip to content Skip to sidebar Skip to footer

Python - Receiving "valueerror: Invalid Literal For Int() With Base 10: 'a'" When Testing Code In Jupyter Notebook

I'm trying to build a vaccine tracker for my apartment complex. When I'm trying to test the below code in jupyter-notebook I receive following error when I use non integer inputs.

Solution 1:

You will need an error checking for your input before passing the input into the next steps.

A common practice is to use a loop with try-except to wrap the prompt until the input is valid.

whileTrue:
    try:    
        vaccine_type = int(input("Select a number - "))
        if vaccine_type != 1and vaccine_type != 2:
            raise ValueError
        breakexcept ValueError:
        print("Invalid Input - Please enter either 1 or 2 to proceed further")
...

Solution 2:

Please read a bit documentation before you use any functions. If you are not aware of what a function is, read here.

The error or exception(not normal behaviour) which you are getting is because while passing input to in-built input function, you are passing the character a, then the in-built function int tries to convert this string a to integer with base 10. Since a is not a valid integer, hence the python interpreter throws this exception/error.

Please use checks as burringalc recommended in other answer and make sure it could be converted to a valid integer or make sure you only pass input as 1 or 2, since you only wish to check for 1 or 2 as an input.

Post a Comment for "Python - Receiving "valueerror: Invalid Literal For Int() With Base 10: 'a'" When Testing Code In Jupyter Notebook"