1

I Need ['1', '2', '3'] to become this [1, 2, 3]

This is my actual code:

def chain_a_list_int(p_chain :str):
   tab_chain=[] # [str]
   tab_int=[] # [int] (list to return)

   tab_chain = p_chain.split(",")
   tab_chain = [int(i) for i in tab_chain]
   tab_int.append(tab_chain)
   return tab_int

and return it to use it like that:

chain_a_list_int(input("enter the number to conserve: "))

but it give me this error when i print it out of the function:

<function chain_a_list_int at 0x000000000349FEA0>

when i try to print i use this:

 print(chaine_a_liste_entier)

when i print tab_int in the function it work pretty well but it's not when i'm out of it...

print(tab_int)

Result...

[1, 2, 3]
cxw
  • 16,685
  • 2
  • 45
  • 81
  • 6
    Or even `map(int, p_chain.split(','))` – ForceBru Oct 07 '16 at 16:18
  • That's not an error, but an indication that Python doesn't think that you have asked it to call `chain_a_list_int`. Would you please [edit your question](https://stackoverflow.com/posts/39921730/edit) and add a bit more of the code you are using around what you've shown? For example, please add the `print` statement you are using. – cxw Oct 07 '16 at 16:21
  • PS welcome to the site! Check out the [tour](https://stackoverflow.com/tour) for more on asking questions in a way that will attract quality answers. – cxw Oct 07 '16 at 16:22
  • @ForceBru how do i use this line?? (where should i put it) – Olivier Turcotte Oct 07 '16 at 16:30
  • @OlivierTurcotte, this is the solution. You could also do `map(int, input("enter the numbers, comma-separated: ").split(','))` _instead_ of all of your current code. – ForceBru Oct 07 '16 at 16:38

2 Answers2

1

That's not an error, but an indication that Python doesn't think that you have asked it to call chain_a_list_int. The minimal tweak to your code is:

the_list = chain_a_list_int(input("enter the number to conserve: "))
print(the_list)

or

print(chain_a_list_int(input("enter the number to conserve: ")))

A reference to the name of the function chain_a_list_int, without a ( after it, does not actually cause the function's code to run. This distinction will be useful to you later on — for now, make sure any time you type the name of a function, you put a parenthesized expression after that name. (If @ForceBru posts an answer, you'll see a counterexample :) .)

cxw
  • 16,685
  • 2
  • 45
  • 81
  • Can you please check part 2 of my probleme? – Olivier Turcotte Oct 07 '16 at 17:31
  • @OlivierTurcotte If you have two separate, unrelated problems, you are required to ask them as two separate questions. You should edit out anything from this question that's not relevant, and ask it as another one. – Random Davis Oct 07 '16 at 17:42
  • 1
    @OlivierTurcotte to follow up on what Random Davis said, asking a second question rather than editing an existing question makes it easier for future readers to benefit from the questions and answers. Also, someone else may have a better answer to Part 2 than I would! Asking a separate question for Part 2 makes it more likely that other folks will also be able to help. – cxw Oct 07 '16 at 17:44
  • @cxw alright, for the same question then, when i print the result it return me [[1,2,3]] so its not recognized as a int – Olivier Turcotte Oct 07 '16 at 17:49
  • @OlivierTurcotte Missed that one - instead of `tab_int.append(tab_chain)`, use `tab_int.extend(tab_chain)` per [this](https://docs.python.org/3/tutorial/datastructures.html#more-on-lists). Or, better yet, just `return tab_chain` :) – cxw Oct 07 '16 at 18:10
0

You need to call the function to print its output, ie print(chaine_a_liste_entier('1,3,5,6')).

CSTobey
  • 1,491
  • 13
  • 9