In Python, there is an exception handling for the program to be able to recover after an error is detected and continue to execute it from the same place. This is one way to control the user input.
Suppose your program requests an integer, and the user enters a string in response. By default, in such cases, the program displays an error message, and then stops. Exception handling will help you solve the problem without restarting the program.
Example 1. Introduce in the code of the previous lesson processing from the input error by the user not a number, let's say a letter, a word or another symbol.
try:
block = int (input ("Type block type"))
position = mc.player.getTilePos ()
x = position.x
y = position.y
z = position.z
mc.setBlock (x, y, z, block)
except:
mc.postToChat ("This is not a number! Ai Yay! Next time, enter a number!")
This program asks the user for a number. If, instead of numbers, you enter letters or signs, it will print in the Minecraft chat: ("This is not a number! Ay !, next time, enter a number!"). The error occurs because int () can only process strings that contain integers