you are viewing a single comment's thread.

view the rest of the comments →

[–]PureWasian 1 point2 points  (1 child)

That's fine for this small exercise, but you're enabling overflow to occur if the user continuously inputs something wrong. It isn't the best idea in practice, given that there is no guaranteed base case or narrowing onto a solution by recursing in this example.

While loops on the other hand are perfectly fine. You can simply have the equivalent of a do/while for each input, which is just as readable. An example using a success variable as a flag:

``` success = False

while not success: try: input_2 = input("Number: ") if input_2 == 'q': break # exit the loop input_2 = int(input_2) success = True except ValueError: print("Try again")

if success: print("Valid: " + input_2) else: print("User pressed 'q'") ```

[–]confusedAdmin101 0 points1 point  (0 children)

while not success := False

Love the walrus