1

In my code below, I am trying to print all numbers in a string. The output prints 10, 20, 30, None. I understand the "None"is printed because I am not "return"anything in the function. How do i best re-write this code to avoid "none" being output.

import re

def myfunc(string):
    patterns=r'\d+'
    array=re.findall(patterns,string)
    for n in array:
        print(n)

print(myfunc("Ten 10, Twenty 20, Thirty 30"))
kaya3
  • 47,440
  • 4
  • 68
  • 97
  • 2
    Just call `myfunc("Ten 10, Twenty 20, Thirty 30")` instead of `print(myfunc("Ten 10, Twenty 20, Thirty 30"))`, if you don't want to print the `None` that it returns. – kaya3 Jan 29 '20 at 04:31
  • You are printing the return statement of the `myfunc` function, which is `None`... – Seraph Wedd Jan 29 '20 at 04:33

1 Answers1

2

The cause is that a return is missing from the function.

Otherwise, there is an implicit return None in every function.

Either leave-off the bottommost print() or have the function return a useful value.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • 3
    Perhaps refrain from answering obvious FAQs and instead close as a duplicate. – tripleee Jan 29 '20 at 04:34
  • 1
    @triplee For new posters, how about erring on the side of being nice. You don't have to be harsh to newcomers or you don't have to edit-out minor pleasantries. – Raymond Hettinger Jan 29 '20 at 04:36
  • Duplicates are not "harsh", they typically have several well-vetted answers that probably help the newcomer more than a tired routine recapitulation of the standard answer. – tripleee Jan 29 '20 at 04:39
  • @tripleee From the point of view of the OP, they likely didn't know enough to find or recognize the other answer. I don't object this being closed as a duplicate, but it wasn't wrong for me to help with his or problem, nor was it wrong to be nice about it. We're trying to make StackOverflow a better place and to give especially welcoming to newcomers. You don't have to be on-board with that, but you don't have to stomp on others who are trying to make the environment more pleasant. – Raymond Hettinger Jan 29 '20 at 04:43