There is such a code:

print("Hello, user! This programm would find the roots of quadratic equation Ax^2 + Bx + C = 0") A = input() B = input() C = input() if (A.isdigit()) and (B.isdigit()) and (C.isdigit()): D = int(int(B)*int(B) - 4*int(A)*int(C)) import math if D > 0: x1 = float((-int(B) + math.sqrt(D))/(2*int(A))) x2 = float((-int(B) - math.sqrt(D))/(2*int(A))) print("Two roots: x1 = ", x1, ", x2 = ", x2) elif D == 0: x = float(-int(B)/(2*int(A))) print("One root: x = ", x) else: print("No roots.") else: print("Wrong input format.") 

In the case when we consider that ABC are integers, this code checks input ABCs for correctness. And how can you verify the correctness of the input data (suppose that the user does not enter the text) for real ABC?

    3 answers 3

    I added this structure when entering:

     try: A = float(input()) B = float(input()) C = float(input()) except ValueError: print("Wrong input format, try again. \n") 

    Testing empirically, I can say that this option is similar to the truth. But not sure that all possible situations are taken into account.

      You can use regular expressions . (^\d+\.\d+$)|(^\d+$) pattern finds all strings that contain any number of numbers or which contain a number, a point, and a number.

       import re pattern = r'(^\d+\.\d+$)|(^\d+$)' #задает шаблон поимка pattern = re.compile(pattern) a = input() if pattern.match(a): a = float(a) else: print('Введено не допустимое значение') 

        @FrostahG, IMHO will be more complete:

         while True: try: A = float(input('Input "A": ')) B = float(input('Input "B": ')) C = float(input('Input "C": ')) except ValueError: print("Wrong input format, try again. \n") else: break