Categories :

Loops, conditions and exceptions in Python Part 3

If the condition is false, the interpreter moves to the next unit in line, in our case — the string with the keyword else. This word tells the interpreter that if the condition in if is not executed, you should execute the code inside the else block:. In this block I put the two operations that were previously inside the block while True:.

Thus, the program appears the condition: if the symbol is q, then the work cycle should be completed, if not, continue to transfer a variable number of type string to type int and display the message with the square of the number.

Image result for conditions in programming

Surely many will ask — and what I learned for the transfer of the number variable of the type string to type int on a separate line inside the else block:? The fact is that if you leave this operation for the first row cycle, after entering the character q, the program will issue an error, because the symbol q cannot be converted to int — that is to make an integer. In other words, the program will fail before you have time to check the condition. If we remove migrate inside the else block, the input of q will exit the program. It remains only to make the typing of other letters do not lead to the error.

So the program did not throw a user every time he enters a non-integer number and any letter except q, we use the exception handling statements try / except — and describe how they work.

Design with try and except became part of the else block: loop while True:, but because both words are typed indented eight spaces, and the lines inside the try: and except ValueError: — 12 spaces.

Try the word indicates to the interpreter that it is necessary to check the feasibility of the statements within the block that follows the colon. If they are feasible and do not generate errors, then you need to follow them and return to the previous block of code (in our case the block while True:), if impossible to go on the block with the keyword except.

Block words except specifies what to do in that case, if the operation from try block: proved impracticable. In our case we expect an error like ValueError and specify it after the keywords — although you could just write except:, that is, what to do if you receive an error of any type.

Thus the program checks that the user entered. If he entered a non-integer number and any letter except q, the program, realizing that you cannot run the first line of the try block: displays an error message from the except block: and continues the cycle to continue. Now, if you enter the wrong character, the program will suggest you try to do it again, and not stop working.

Now the program does everything the way I wanted in the first place. It can be expanded — for example, to calculate not only square, but cube numbers entered or worked with decimal fractions, for which it is necessary to use the data type float. I do this blog will not, but if you suddenly have the desire to go forward.

Image result for exceptions in programming

Maybe I overdid it today with new material and this will all be difficult to digest. But I deliberately did not go into each of those, and only raised them: we will meet these concepts and get the hand how to use them. Until I decided that it would be in the next release — I think either a simple text game, or the assignment I gave myself in the first issue of the blog. The important thing is that next time, we will strengthen and extend the representation obtained in this and the previous release.

Please, if you do not understand something, write comments under the post and in social media — I will be glad and any other feedback. If you are more experienced in programming people than I, with pleasure will listen to substantive criticism. Thanks, and until next time!