Is infinite loop better than recursive calls when asking for input?

Hi all!

So I’m doing the following project:
https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-102/modules/graphs-and-graph-search/projects/skyroute-graph-search

and by the 12th step you get to request user input.

Usually by now in the CS path, when asking for user input you’d open a while loop that becomes false when the user finally provides a correct input.

In this case, a more “risky” way is suggested - by recursive call the same function until the user gets it right.

So I decided to have fun and overflow the site’s stack:

Sure, it did take a bit, but won’t it be safer to use a loop instead? or from a user’s perspective it’s better the software crashes rather than stuck in an infinite loop?

In this situation a conditional loop would be simpler and less likely to create maintenance issues.

Let’s say you have multiple inputs in the program. The thing to do would be create a general function that takes a prompt and a range or selection of valid inputs, and then runs until the input is within range or meets the condition.

1 Like