x = input("Insert numbers only: ") while type(x) != float: try: x = float(x) except ValueError: print("Error! Please insert only numbers...") x = input("Insert numbers only: ") print(x/2)
Output:
Insert numbers only: 123d Error! Please insert only numbers... Insert numbers only: 123 Insert numbers only: 3 Insert numbers only: 78 Insert numbers only: hfg Error! Please insert only numbers... Insert numbers only:
100+ Python Pattern Examples