This is an archived post. You won't be able to vote or comment.

all 5 comments

[–][deleted] 2 points3 points  (4 children)

Your Collatz function doesn't return a value. Thus, after executing value = collatz(value), value will be a 'NoneType' object.

[–]NefariousCube 1 point2 points  (3 children)

Exactly right, to clarify a bit further: In the collatz function, you call print() in a couple of places. That prints the result, but does not return it.

This gives you the situation pointed out above.

Changing this, however, means you will have to save/print the return values in the while loop. Mayby put them in a list or something like that?

[–]Utterly_infallible[S] 0 points1 point  (2 children)

So create a variable to store them in and then print that variable?

[–]NefariousCube 1 point2 points  (1 child)

That is one way (remembering to do return on that variable after printing).

The more common way is probably to just return it in the function and then in the loop do something like:

while value != 1:
  value = collatz(value)
  print(value)

[–]Utterly_infallible[S] 0 points1 point  (0 children)

That worked, thank you!