I’m trying to make an algorithm in python that asks you to insert a number and then it prints the hundreds, dozens and units from the number inserted.
What I was thinking to do is to extract the character from the number that corresponds to the hundreds and print it as hundreds, as well with the dozens and units.
For instance, what I’ve tried to do so far was:
number = 321
print 'Hundreds: ', number[1]
However, when I tried to run that, I got the message:
TypeError: 'int' object is not subscriptable
Is it impossible to do what I want?
Try
str(number)[1]. You first have to convert the number to a string.Note that this is not the most elegant solution. You might want to try
(number // 100)%10instead. Therefore,numberhas to be anint. The//denotes integer division.