adhamncheese
adhamncheese

Reputation: 111

How can I print my outcome as a list?

I've created a program where the output prints as, for example, 36, 6, 2, 1.

I want it to print [36, 6, 2, 1].

This is my current code:

def collatz(n):

    print(n, end = ', ')

    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            print(1)
        else:
            print(n, end = ', ')

I am not sure what to edit at this point as I've tried a lot messing with the print statements and I have seen in other posts where print(*n, ...) was used but I get the error:

TypeError: print() argument after * must be a sequence, not int.

Which I get why it wouldn't work so I'm lost at this point.

Upvotes: 2

Views: 321

Answers (4)

Anthony E
Anthony E

Reputation: 11235

You can simplify your function considerably by getting rid of the if condition since

if n % 2 == 0:
  n=int(n**0.5)
else:
  n=int(n**1.5)

Is the same as: n % 2 + 0.5

And also using recursion rather than iteration. So your collatz function can simply implemented as this:

def collatz(n):
  if n <= 1:
    return [1]
  return [n] + collatz(int(n ** (n % 2 + 0.5)))

collatz(n) will now return an array which is fine, its better if functions have a single responsibility. Now you can wrap it in a function which prints the array in a Python-like format:

def print_collatz(n):
  return str(collatz(n))

So you can simply call:

print_collatz(36)
# [36, 6, 2, 1]

To return an array-formatted string:

Upvotes: 1

Bonifacio2
Bonifacio2

Reputation: 3840

You can create a list and print its string representation:

def collatz(n):

    my_list = []

    my_list.append(n)

    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            my_list.append(1)
        else:
            my_list.append(n)

    print(str(my_list))

Upvotes: -1

Burhan Khalid
Burhan Khalid

Reputation: 174614

To control how its printed, simply print the [ and ] separately. Don't confuse how its printed with how its stored or what data type it has.

def collatz(n):

    print('[{}'.format(n), end=', ')

    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            print(1, end=', ')
        else:
            print(n, end = ', ')
    print(']', end='')

Upvotes: 2

Bhargav Rao
Bhargav Rao

Reputation: 52071

The best way would be to create a list and append the values of n to the list. In this way you can take the advantage of the builtin functionality of str(list) which automatically adds [ and ] to the end while printing.

A sample code can be

def collatz(n):

    templist = [n]    
    while (n > 1):
        if n % 2 == 0:
            n=int(n**0.5)
        else:
            n=int(n**1.5)
        if n == 1:
            templist.append(1)
        else:
            templist.append(n)
    print(templist)

Now when you run collatz(36) you get [36, 6, 2, 1]

Upvotes: 1

Related Questions