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

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to