Categories :

Loops, conditions and exceptions in Python Part 2

Attention! Only that I brought not very literate code, or rather — not very code. Although the interpreter would accept it, the design that I used, rather typical for other languages, e.g., C (although, in my opinion, still not very elegant). In Python to specify how many times the loop should execute the statements within the block, you usually use the cycle counter c the keywords for and in, but we’ll talk about it later.

Image result for conditions in programming

Perhaps someone will ask why I did not placed the line of i = 0 inside the unit? Would it not be wiser, given that outside the block the variable is not used? Besides the fact that it’s just not very neat code, remember that if we had i = 0 the first line inside the block, at the beginning of each cycle the value of i would be reset. I will explain what the erroneous code below:

while i < 3:

i = 0

number = int(input(“Enter an integer: “))

print(“The square of {} is {}.”.format(number, square(number)))


At the beginning of the first cycle the interpreter executes the statement on the first line of the block and assigns the variable i the value 0.

The interpreter comes to the fourth line of the unit, conducts operations zoom in i++ the value of i becomes 1.

The interpreter, seeing that the block is over and the condition i < 3 is still running (1 is really less than 3), begins the second cycle.

Again, the interpreter executes the statement on the first line and assigns the variable i to the current value 1 new value 0 old.

Steps 1-4 are repeated endlessly. In fact, we get poorly written analogue of the while loop to True — the value of i never reaches the not that 3, and even 2.

Although we fixed the first fault, in its place appeared another. Now our program runs endlessly — it is impossible to get out. But what if you set the condition under which the user is typing the letter q would be able to exit the program? Let’s do it, and at the same time analyze the changes in the code:

Note the third and last line. I added a function print() to output messages, which helps to Orient the user in the program. The first message will always be output once at the beginning, the second once, when the program terminates. Inside the unit related to the loop while True: appeared branching statements if and else statements. They allow you to set the conditions for executing code blocks and is equivalent to the syntax “if …, then …”. Here’s how they work:

After the keyword if the condition is set. If this condition is true, then the interpreter jumps to the code inside the block that follows the colon. In our case, the condition is a number == ‘q’ and the entire row will be read as “if the number variable value is ‘q'”. The == operator means “equal” — not to be confused with the operator =, which denotes assignment (condition number = ‘q’ produces the error attempt to assign the variable number the value of which has just been introduced, the value of ‘q’; we need to compare the entered value with the value ‘q’).

Image result for conditions in programming

Within the block use the break keyword, telling the interpreter that he needed to interrupt the current cycle and move to the next block for line of programs — in our case this is the last line of the program is with the print () function. If … unit is located inside the unit while True:, but because the row with the keyword break written indented eight spaces. If word break is not there, then the interpreter, passing the else block: continued to move further along the cycle.