[Tutor] newline problem
bob gailer
bgailer at gmail.com
Sat Aug 21 21:09:04 CEST 2010
On 8/21/2010 2:36 PM, Roelof Wobben wrote:
> Hello,
>
> I have this programm :
>
> def print_digits(n):
> """
> >>> print_digits(13789)
> 9 8 7 3 1
> >>> print_digits(39874613)
> 3 1 6 4 7 8 9 3
> >>> print_digits(213141)
> 1 4 1 3 1 2 """
>
> count = 0
> while n:
> count = count + 1
> n = n / 10
> return count
> getal=13789
> x=print_digits(getal)
> teller=1
> while teller <=x :
> digit=float(getal/10.0)
> digit2=float(digit-int(digit))*10
> getal=digit
> print int(digit2)
> teller=teller+1
>
>
> But now every digit is placed on another line.
> How can I prevent this ?
print int(digit2), # add a comma
Your algorithm is unnecessarily complicated.
You don't need to count first.
You don't need float.
getal=13789
while getal > 0:
getal, digit = divmod(getal, 10)
print digit,
But it is easier to just:
print " ".join(str(getal))
--
Bob Gailer
919-636-4239
Chapel Hill NC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100821/cc74635d/attachment.html>
More information about the Tutor
mailing list