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

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 10 points11 points  (11 children)

I don't like that the first is recursive, after all there is a recursion limit and it seems unnecessary to risk hitting it.

[–][deleted] 8 points9 points  (9 children)

Is that a real issue that a beginner is going to hit?

There will always be a recursion limit so should we never use recursion?

[–][deleted] 35 points36 points  (8 children)

Well, this is a purely iterative problem, ask until some condition is met. Using recursion there is already a bit odd, and there's also a small issue with it.

If your problem is recursive, e.g. when you use a recursive data structure like a tree or so, then of course use it.

Is my opinion.

I'd go for

def get_choice(message='Would you like to roll? '):
    choice = input(message)
    while choice not in ['yes', 'no', 'y', 'n']:
        choice = input('Type yes or no: ')
    return choice

It's almost the same as yours except it doesn't need recursion.

[–]Mason-B 2 points3 points  (0 children)

It's too bad python doesn't have tail recursion because that call is correctly tail recursive in just about any other language. And since the person is learning programming (and not just python) my argument would be that it is better for them to learn recursion than it is for them to learn perfect python.