#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    PYTHON 3.1 i have an Int number to single digits func,returns float , need integer


    The func:

    def int_digits(n):
    return [n] if n<10 else int_digits(n/10)+[n%10]

    The Code:

    x=1
    while x==1:
    name = str (input("Enter anything:"))
    print("This is the memory cell status of your string : '"+name+"'")
    print ('')
    sz = len(name)
    y=0
    while sz>y:
    add = name
    hexnum=(hex(id(add)+y))
    print(" Letter : "+str(y+1))
    print('______________________________________________')
    print(hex(id(add)+y))
    a=int (hexnum,16) # HEX to DEC
    print()
    print(a)
    print (int_digits((a))) #DEC to ASCII
    print(a)
    a=int (hexnum,16)
    a= dec_to_bin(a) #DEC to BIN
    print (a)
    y=y+1
    print('______________________________________________')
    x = int (input("press \n 0 to EXIT \n 1 Retry: "))


    print('BYE')
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    474
    Rep Power
    33
    Where is the float coming from? You didn't say. int_digits returns a list of integers
    Code:
    def int_digits(n):
       return [n] if n<10 else int_digits(n/10)+[n%10]
    
    for num in [1, 12, 123]:
        print int_digits(num)
    
    """ returns
    [1]
    [1, 2]
    [1, 2, 3]
    """
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Division defaults to true in python 3 which he stated he was using.

    You need to use "//" instead of "/" to force integer division in python 3.
    Code:
    def int_digits(n):
       return [n] if n<10 else int_digits(n//10)+[n%10]
    
    for num in [1, 12, 123]:
        print(int_digits(num))
    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo