Reverse Iteration Through Integers

Rhodri James rhodri at wildebst.demon.co.uk
Sun Oct 18 23:51:35 CEST 2009


On Sun, 18 Oct 2009 19:34:09 +0100, Benjamin Middaugh  
<benjamin.the.violinist at gmail.com> wrote:

> I'm trying to make an integer that is the reverse of an existing integer  
> such that 169 becomes 961. I guess I don't know enough yet to figure out  
> how to do this without a ton of awkward-looking code. I've tried for  
> loops without much success. I guess I need a good way of figuring out  
> the length of the input integer so my loop can iterate that many times  
> to reverse the number, but I keep getting errors that say "TypeError:  
> 'Int' object is not iterable".

Alternatively, turn your integer into a string, reverse the string, and
turn it back:

rhodri at gnudebst:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> n = 123
>>> int(str(n)[::-1])
321



-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list