In finding this error there are few lessons to think about. First, you may find it very disconcerting that you cannot understand the whole program. Unless you speak Polish then this won’t be an issue. But, learning what you can ignore, and what you need to focus on is a very important part of the debugging process. Second, types and good variable names are important and can be very helpful. In this case a and x are not particularly helpful names, and in particular they do not help you think about the types of your variables, which as the error message implies is the root of the problem here. The rest of the lessons we will get back to in a minute.
The error message provided to you gives you a pretty big hint. TypeError: unsupported operand type(s) for FloorDiv: 'str' and 'number' on line: 5
On line five we are trying to use integer division on x and 24. The error message tells you that you are tyring to divide a string by a number. In this case you know that 24 is a number so x must be a string. But how? You can see the function call on line 3 where you are converting x to an integer. int(x)
or so you think. This is lesson three and is one of the most common errors we see in introductory programming. What is the difference between int(x)
and x = int(x)
The expression int(x)
converts the string referenced by x to an integer but it does not store it anywhere. It is very common to assume that int(x)
somehow changes x itself, as that is what you are intending! The thing that makes this very tricky is that int(x)
is a valid expression, so it doesn’t cause any kind of error, but rather the error happens later on in the program.
The assignment statement x = int(x)
is very different. Again, the int(x)
expression converts the string referenced by x to an integer, but this time it also changes what x references so that x now refers to the integer value returned by the int
function.
So, the solution to this problem is to change lines 3 and 4 so they are assignment statements.