1

I am writing a function to test if a string length is odd or even, but running into some issue when I try to test the result. I get back the result odd, and None. I need it run for the test result but it's testing my function value. What am I missing here? Thanks.

Below is the function:

    def strEorO(inputVal):

       if len(inputVal) % 2 == 0:
          print('even')
       else:
          print('odd')

Here is where i am testing the function:

   result = strEorO('airplane')
   print(result)
Joe 5
  • 29
  • 7
  • 1
    That is because `print` is not same as `return`. Instead of `print` within the function, use `return`. i.e. `return 'even' if len(inputVal) % 2 == 0 else 'odd'` – shahkalpesh Jul 24 '21 at 19:20
  • 2
    That's because you don't return anything from the function. Change your *print*s to *return*s (e.g. `return "even"`). – CristiFati Jul 24 '21 at 19:21
  • Got it. So i need to change the print even or print odd to return even or return odd. – Joe 5 Jul 24 '21 at 19:25

1 Answers1

2

As was discussed in the comments, your function isn't returning anything, so printing the result will print None. Use either of these two methods instead:

Return the Output, then Print:

def evenOrOdd(string):
  if len(string) % 2 == 0:
     return "Even"
  else:
     return "Odd"

result = evenOrOdd('hello')
print(result)

Return nothing, print within function:

def evenOrOdd(string):
   if len(string) % 2 == 0:
      print('Even')
   else:
      print('Odd')

evenOrOdd('hello')
John Sorensen
  • 710
  • 6
  • 29