James W. answered 06/23/23
MIT Math and Computer Science Teacher
Hi,
Please see the attached code and let's look at each step...
(1) we define a function that achieves the task of reversing the input
(2) In this function we first convert the input to a string with str(inp)
(3) Now that it's a string we can iterate through it using list notation.
You may be familiar with something like this my_list[0:10] => this will read the first 10 items in the list named my_list. But there is an implicit "one-at-a-time", so equivalently my_list[0:10:1] and my_list[0:10:2] will read every other item. So by using str(inp)[::-1] we are reading each item in steps of -1 (ie in reverse).
(4) finally we return our result cast as an integer int(str(inp)[::-1])
(5) we ask the user for the input
(6) and we push that input into our function
Hope this makes sense!