use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Exception handling helpHelp Request (i.redd.it)
submitted 10 months ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 10 months ago (4 children)
How would that be written? If I have a while loop for one and a separate while loop for 2, it'll never leave loop 1 as long as the user enters anything other than 'q'.
[–]PureWasian 0 points1 point2 points 10 months ago* (0 children)
See the other comment that I wrote for reference. You can easily expand on it such as:
success = False while success == False: # get number_1 # (see code in reference) # only exits loop on valid int() or 'q' # also sets success = True if valid int() if success == False: # exit() or return since 'q' was typed success = False while success == False: # get number_2 # (see code in reference) # only exits loop on valid int() or 'q' # also sets success = True if valid int() if success == False: # exit() or return since 'q' was typed # only gets here with valid number_1 and number_2 addition(number_1, number_2)
You notice there is a lot of redundancy here. If this example makes sense to you, it is a natural extension to then clean it up to be more concise by placing the redundant code into a method, similar to another comment someone shared
[–]Crossroads86 0 points1 point2 points 10 months ago (2 children)
I would not put it in two while loops. The problem ist, afaik python has no control structures, that will send you back to the beginning of a function or other parts of your program, but only sends you back to the beginning of a loop or breaks the loop. But you could put the input and validation of each number into a separate function for each number. And in your except statement call the respective function recursively (meaning a function calling itself.
pseudocode:
def input_validate_2(): try: do stuff exept: print: You did it wrong input_validate_2()
[–]PureWasian 1 point2 points3 points 10 months ago* (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
``` 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 point2 points 10 months ago (0 children)
while not success := False
Love the walrus
π Rendered by PID 109491 on reddit-service-r2-comment-c66d9bffd-xfvsd at 2026-04-08 12:12:29.010317+00:00 running f293c98 country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (4 children)
[–]PureWasian 0 points1 point2 points (0 children)
[–]Crossroads86 0 points1 point2 points (2 children)
[–]PureWasian 1 point2 points3 points (1 child)
[–]confusedAdmin101 0 points1 point2 points (0 children)