473,594 Members | 2,885 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 20430
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
10534
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
9720
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
96283
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
15964
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
5697
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
12342
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
7947
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8374
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
8010
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
5413
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
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
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
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.