How can I print my outcome as a list?

adhamncheese

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.

Bhargav Rao

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]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I print my outcome as a list?

From Dev

How can I print a list with my own type of elements in OCaml?

From Dev

How can I list and print files in a directory?

From Dev

How can i print a list without the brackets?

From Dev

How can I print a list in columns?

From Dev

How can i print a list of arrays in JAVA?

From Dev

How can I format a list to print?

From Dev

How can I print something if an item is in a list?

From Dev

How can i print my array and my histogram on the same line?

From Dev

How can I list the messages in my mailbox?

From Dev

How can I change the view of my list?

From Dev

Awk print $0 print all lines of my files to the last position of column ;how I can print at first?

From Dev

How can I print my array in a certain way?

From Dev

How can I check if my function print/echo's something?

From Dev

How can I get my program to print floats?

From Dev

How can I print data from my database while in QTableWidget?

From Dev

PHP - How can I print out my database table?

From Dev

how i can print screen a single part of my layout?

From Dev

How can I print this?

From Dev

How can I print out a list of the keys in a hashmap?

From Dev

How can I do a recursive print using class linked list

From Dev

How can I print the same list twice without empty it?

From Dev

How can i remove a node from this list and then print it?

From Dev

How can I tell if my <List> is being used by my program?

From Dev

How can i center the list items inside my unordered list?

From Dev

How can i center the list items inside my unordered list?

From Dev

how can i change my Datagrid column header text and how can i print updated header in excel?

From Dev

How can i convert a dataset with ratios for a binary outcome to something suitable for logistic regression in R

From Dev

How can I count lines of differently named files, and write the outcome to a csv file?

Related Related

  1. 1

    How can I print my outcome as a list?

  2. 2

    How can I print a list with my own type of elements in OCaml?

  3. 3

    How can I list and print files in a directory?

  4. 4

    How can i print a list without the brackets?

  5. 5

    How can I print a list in columns?

  6. 6

    How can i print a list of arrays in JAVA?

  7. 7

    How can I format a list to print?

  8. 8

    How can I print something if an item is in a list?

  9. 9

    How can i print my array and my histogram on the same line?

  10. 10

    How can I list the messages in my mailbox?

  11. 11

    How can I change the view of my list?

  12. 12

    Awk print $0 print all lines of my files to the last position of column ;how I can print at first?

  13. 13

    How can I print my array in a certain way?

  14. 14

    How can I check if my function print/echo's something?

  15. 15

    How can I get my program to print floats?

  16. 16

    How can I print data from my database while in QTableWidget?

  17. 17

    PHP - How can I print out my database table?

  18. 18

    how i can print screen a single part of my layout?

  19. 19

    How can I print this?

  20. 20

    How can I print out a list of the keys in a hashmap?

  21. 21

    How can I do a recursive print using class linked list

  22. 22

    How can I print the same list twice without empty it?

  23. 23

    How can i remove a node from this list and then print it?

  24. 24

    How can I tell if my <List> is being used by my program?

  25. 25

    How can i center the list items inside my unordered list?

  26. 26

    How can i center the list items inside my unordered list?

  27. 27

    how can i change my Datagrid column header text and how can i print updated header in excel?

  28. 28

    How can i convert a dataset with ratios for a binary outcome to something suitable for logistic regression in R

  29. 29

    How can I count lines of differently named files, and write the outcome to a csv file?

HotTag

Archive