Implementation of the linear equation solving algorithm in Python

 <<algorithm

example of an algorithm solving the linear equation

a = float(input('a = '))
b = float(input('b = '))
if a == 0:
    if b == 0:
        print('Identity equation')
    else:
        print('Contrary equation')
else:
    x = -b/a
    print('x =', x)

 

 

On line 1 of the program, we use the input() function to read a floating-point number (type float) from the standard input (keyboard), which we immediately store in the variable a. The input function simultaneously displays the text provided as its argument on the screen. On line 2, we analogously ask the user to provide the second number, which we assign to the variable b. Lines 1 and 2 implement the Input/Output operations contained in block (2) of the algorithm. The Start block (1) has no direct equivalent in the Python language.

The conditional statement on line 3 is the Decision block (3) of our algorithm. The statement checks whether the coefficient a is equal to 0. If the condition is met, the instructions written with an indentation in lines 4 through 7 are executed (corresponding to the Decision block (4) and Input/Output blocks (5) and (6) in the algorithm). If the condition is not met, the instructions written with an indentation after the keyword else, contained in lines 9 and 10, are executed. First, on line 9, we calculate the solution to the equation x=−b/a (Operation block (7)), and then on line 10, we output the received result (Input/Output block (8)).

This is where our program ends. In Python, there is no separate instruction to indicate the program's termination.