473,503 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing the digits of an integer

3 New Member
I want to write a simple function that prints the digits of an integer in reverse.
I think it could be done with the while loop, but I am not so sure.
I have tried this, but it didn't work.

Expand|Select|Wrap|Line Numbers
  1. def print_digits(n):
  2.     n = abs(n)
  3.     while n > 0:
  4.         n = n % 10
  5.         print n,
  6.  
Jul 23 '08 #1
4 20405
jlm699
314 Contributor
In python % is the modulo operator... I'm not sure what you are trying here...

Here's how to reverse the digits of an integer using list comprehension:
Expand|Select|Wrap|Line Numbers
  1. >>> def print_digits(n):
  2. ...     new_n = [lett for lett in str(n)]
  3. ...     new_n.reverse()
  4. ...     print ''.join(new_n)
  5. ...     
  6. >>> print_digits(136)
  7. 631
  8. >>> 
Jul 23 '08 #2
jlm699
314 Contributor
Oh wait.. now I see what you were trying to do...
Expand|Select|Wrap|Line Numbers
  1. >>> def print_n(n):
  2. ...     while n > 0:
  3. ...         print n % 10,
  4. ...         n = n / 10
  5. ...     
  6. >>> print_n(492)
  7. 2 9 4
  8. >>> 
Modulus 10 will give you the remainder but you want to integer divide by 10 each time to reduce the number another digit.
Jul 23 '08 #3
verbtim
3 New Member
Well, I am new to Python and I am reading this:
http://openbookproject.net/thinkCSpy/ch06.xhtml#auto16
so the exercise is number 8
Expand|Select|Wrap|Line Numbers
  1. def print_digits(n):
  2.     """
  3.       >>> print_digits(13789)
  4.       9 8 7 3 1
  5.       >>> print_digits(39874613)
  6.       3 1 6 4 7 8 9 3
  7.       >>> print_digits(213141)
  8.       1 4 1 3 1 2
  9.     """
  10.  
Hope this helps!
Jul 23 '08 #4
verbtim
3 New Member
Thanks for the quick reply. I can now sleep in peace.
Jul 23 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3128
by: Michael Hudson | last post by:
Does anyone (Tim?) have (ideally Python, but I can cope) code implementing the fixed-format algorithm from the Subject: named paper by Burger & Dybvig? Cheers, mwh -- <Erwin> #python FAQ:...
6
10522
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
4
9711
by: italia | last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal Places = 6. I had decimals in the original field. But after the transfer, the digits after the decimals are gone. Now...
7
96255
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
13
15941
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
1
5682
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
18
12269
by: Michel Rouzic | last post by:
I can't think of a simple way to print integers with a fix number of digits, in order to obtain something like 0001 for 1 and 0100 for 100 automatically just by specifying in a variable the number...
2
2959
by: ericnyc | last post by:
Hi, I am a newbee in C++ Please review this is what I wrote , what so ever I understood so far, My question is that I have to " Write a program C++ array that reads in an integer number and...
7
2369
by: Ouroborus777 | last post by:
I've been messing around with printing floats. It seems that printf() is only capable of printing the fractional portion at a fixed length. Is there some way to print floats such that the full...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.