473,657 Members | 2,550 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 20439
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
3135
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: How do I build X? A: Wait for twisted.X. -- from Twisted.Quotes
6
10541
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 differences between the databases servers ? Peter
4
9722
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 even after I have change the Fieldsize propert to Decimal with Scale = 2, the digits after the decimal are not seen. For eg. If the text was 16.27. After I changed to long integer, it
7
96293
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 %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
13
15971
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 reason I ask is that I am programming on a RISC system where division is quite expensive, and I want to be able to extract the digits from an integer (the integer will be from 1 to 6 digits long, and I know how many digits are in the number).
1
5704
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 of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me to use the above controls, example 1. The bottom two subroutines will print a graphic file, example...
18
12358
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 of desired digits. I thought about printing to a variable using for example %4d and then replace ' ' by '0', but I don't know if i can print to an array, and then I don't think I can put a variable between % and d to specify the number of...
2
2964
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 checks whether any of the digits in the number appear more than once" Hint: Use an array called digits_seen of size 10 whose base type is Boolean. It will be indexed from 0 to 9, which corresponds to the 10 possible digits and initially all the...
7
2373
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 fraction is shown but without the trailing zeroes? I realize that this could be an exercise in string manipulation (ie: stringify the float giving a length that's longer than the float could possibly be then trim the trailing zeroes) but I would...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.