Validated user input in Python

29

1014

This is very useful since it helps to make a good and clean code without too many errors to fix, for example:

while  True:
        try:
            q=int(input(“Please enter a non-negative integer number: “))
            break
        except ValueError:
            print(“this is not a integer number, try again: “)

 

while q
        try:
            q=int(input(“this is not a positive number,try again: “))
        except ValueError:
            print (“this is not a integer number, try again:  “)
            while True:
                try:
                    q=int(input(“Please enter a non-negative integer number: “))
                    break
                except ValueError:
                    print (“this is not a integer number, try again: “)

 

CC BY 4.0 Validated user input in Python by Jorge Padilla is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.