Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Write a for loop to display a given number in reverse [duplicate]

Tags:

python

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

like image 882
Mike Avatar asked Dec 29 '25 10:12

Mike


1 Answers

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,
like image 192
TessellatingHeckler Avatar answered Jan 01 '26 16:01

TessellatingHeckler