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

4
  • your question is incomplete! Commented Mar 10, 2014 at 1:01
  • what do you mean "in reverse"? Commented Mar 10, 2014 at 1:03
  • What is "reverse"? You want the digits of the base-10 representation of the number from the least to the most significant? What should happen to leading zeros? Commented Mar 10, 2014 at 1:03
  • I think the answer I posted might be helpful Commented Mar 10, 2014 at 1:15

6 Answers 6

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,
Sign up to request clarification or add additional context in comments.

Comments

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='')

1 Comment

upvote for you since I forgot about the inverse slice operator.
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.

2 Comments

The "loop" idea I don't like so much (slicing!) but +1 for not ever casting it to an integer in the first place.
Ah the slicing thing Christian mentions looks a lot better than looping over the characters actually :)
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.

1 Comment

Could you please explain this code in your answer?
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)

Comments

-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

3 Comments

he wants to use loop! Also, in Python 2.7, input takes a string too!
@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
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: stackoverflow.com/questions/14783598/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.