2

How would I have the user input a number and then have the computer spit their number out in reverse?

num = int(input("insert a number of your choice "))
for i in 

That is all I have so far... I am using 3.3.4

Mike
  • 41
  • 1
  • 1
  • 4

6 Answers6

5

Here's an answer that spits out a number in reverse, instead of reversing a string, by repeatedly dividing it by 10 and getting the remainder each time:

num = int(input("Enter a number: "))

while num > 0:
    num, remainder = divmod(num, 10)
    print remainder,

Oh and I didn't read the requirements carefully either! It has to be a for loop. Tsk.

from math import ceil, log10

num = int(input("Enter a number: "))

for i in range(int(ceil(math.log10(num)))): # => how many digits in the number
    num, remainder = divmod(num, 10)
    print remainder,
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
4

You don't need to make it int and again make it str! Make it straight like this:

num = input("insert a number of your choice ")
print (num[::-1])

Or, try this using for loop:

>>> rev = ''
>>> for i in range(len(num), 0, -1):
...     rev += num[i-1]
>>> print(int(rev))

Best way to loop over a python string backwards says the most efficient/recommended way would be:

>>> for c in reversed(num):
...     print(c, end='')
Community
  • 1
  • 1
Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51
1

Why make it a number? 'In reverse' implies a string. So don't cast it to int but use it as string instead and just loop over it backwards.

Stijn de Witt
  • 40,192
  • 13
  • 79
  • 80
  • 1
    The "loop" idea I don't like so much (slicing!) but +1 for not ever casting it to an integer in the first place. – kojiro Mar 10 '14 at 01:04
  • Ah the slicing thing Christian mentions looks a lot better than looping over the characters actually :) – Stijn de Witt Mar 10 '14 at 01:04
1

You've got here a variety of different answers, many of which look similar.

for i in str(num)[::-1]: 
    print i

This concise variation does a few things worth saying in english, namely:

  1. Cast num to a string
  2. reverse it (with [::-1], an example of slicing, a pythonic idiom that I recommend you befriend)
  3. finally, loop over the resultant string (since strings are iterable, you can loop over them)
  4. and print each character.

Almost all the answers use [::-1] to reverse the list -- as you read more code, you will see it more places. I recommend reading more about it on S.O. here.

Community
  • 1
  • 1
gabe
  • 2,521
  • 2
  • 25
  • 37
0

I hate to do your problem for you since you didn't really try to actually solve it or say what you're specifically having a problem with, but:

 num = str(input("..."))
 output = [num[-i] for i in range(len(num))]
 print(output)
kojiro
  • 74,557
  • 19
  • 143
  • 201
aruisdante
  • 8,875
  • 2
  • 30
  • 37
-2
output = input("Insert number of your choice: ")[::-1]
print("Your output!: %s" % output)

In python 3.x+ input is automatically a string, doing [::-1] reverses the order of the string

user3234209
  • 935
  • 2
  • 8
  • 14
  • he wants to use loop! Also, in Python 2.7, input takes a string too! – Sharif Mamun Mar 10 '14 at 01:16
  • @S.M.AlMamun In python 2.7 doesn't raw_input return a string and input() evals it? Also, I'm not sure he wants to use a loop I think he just wants to reverse the string. Thanks for the input though – user3234209 Mar 10 '14 at 01:20
  • 1. Title says that: Python - Write a for loop to display a given number in reverse 2. Difference can be found in the accepted answer here: http://stackoverflow.com/questions/14783598/program-works-in-python-2-7-but-not-python-3-3-syntax-is-compatible-for-both – Sharif Mamun Mar 10 '14 at 01:23