1

I am doing an exercise from hackerrank.com.

My main issue with my code is my function returns none with my while loop.

I know I can pass the exercise by not using it as a function but my personal goal is to maintain it as one. Is there a way I can keep my code as a function and not return None?

My Code

def cutOperation(sticks):
    currentLine = sticks
    while len(currentLine) != 0:
        newLine = []
        print len(currentLine)
        for stick in currentLine:
            if stick - min(currentLine) != 0:
                newLine.append(stick - min(currentLine))
        currentLine = newLine

_ = int(raw_input().strip())
sticks = map(int,raw_input().strip().split(' '))
#First Test Case for sticks = 5 4 4 2 2 8

print cutOperation(sticks)

Result after running code

6
4
2
1
None
#How do I not return none?
AChampion
  • 29,683
  • 4
  • 59
  • 75
KCh
  • 19
  • 1
  • 2

1 Answers1

0

Like others have mentioned in the comments above, you can add a return statement and return whatever you want for your function. Python by default returns None for functions that don't specify a return statement.

If you just don't want to print None for this exercise though, you can simply change:

print cutOperation(sticks)

to

cutOperation(sticks)

since your last line is printing the return of the cutOperation function.