1
def tuplify(number):
    x = str(number)
    if len(x) <= 0:
        return 0
    lst = []
    for i in range(0,10):
        if i == x[len(x)-1]:
            lst.append(i)
        return tuple(lst)

This is my code and I can't get the output I want.

tuplify(102486) which should give me (1, 0, 2, 4, 8, 6)

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
user3398505
  • 597
  • 1
  • 6
  • 11

6 Answers6

5

You can convert that to a tuple of numbers like this

print tuple(map(int, str(102486)))
# (1, 0, 2, 4, 8, 6)

But note that, leading zeros in the number will NOT be retained.

Apart from the fact that you are comparing a string and a number,

if i == x[len(x)-1]:

you are always comparing i with the last element.

Also, you wanted to return a tuple, but if the string is empty, then you are returning zero

if len(x) <= 0:
    return 0

instead, an empty tuple would have been appropriate

if len(x) == 0:
    return ()
thefourtheye
  • 221,210
  • 51
  • 432
  • 478
2

Edited :

>>> p = 123434455
>>> tuple(int(i) for i in str(p))
(1, 2, 3, 4, 3, 4, 4, 5, 5)
>>> 
Prashant Gaur
  • 8,712
  • 10
  • 47
  • 71
1

You have several problems in your code, one is this:

 if i == x[len(x)-1]:

As i is an integer, but x is a string, this is never going to be true. 1 is not equal to "1".

You,r return tuple(lst) is also indented so that it is inside the for loop, but it shouldn't be.

But the for loop is completely strange too -- you loop over numbers 0 to 9, and always compare to the last character of x, why? What is it trying to do?

There are one-liner ways to do this but I don't think those help you.

RemcoGerlich
  • 28,952
  • 5
  • 61
  • 78
0

I would look at :

  1. Converting integer to string in Python?
  2. How to split a string into array of characters with Python?
  3. http://www.u.arizona.edu/~erdmann/mse350/topics/list_comprehensions.html (on map section)

you can cast your number to a string, split it and convert each char back to a int

Community
  • 1
  • 1
nobe4
  • 2,722
  • 3
  • 24
  • 53
0

You have a few issues with your code.

First you don't need to check the length, because there is no such thing as a "blank" number; so what you need to check is if a number was sent or not. Also you are returning 0 which is not a tuple.

Second, you don't need a list because you can step through a string directly.

Finally, your result will be a tuple of strings, when you need a tuple of integers.

Combining this, you get:

def tuplify(number):
    if not number:
       return ()  # this is an empty tuple
    x = str(number)
    return tuple(map(int,x))
Burhan Khalid
  • 161,711
  • 18
  • 231
  • 272
  • Actually `()` is an empty tuple, `(,)` is a syntax error. Confusingly, `(1,)` is the way to write a one-element tuple. – RemcoGerlich Apr 15 '14 at 07:50
0

Try this ,

>>> def tuplify(number):
    t=tuple()
    number=str(number)
    for i in number:
        t+=tuple(i)
    return t

>>> tuplify(123456)
('1', '2', '3', '4', '5', '6')
>>> 
Nishant Nawarkhede
  • 7,757
  • 11
  • 56
  • 79