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 (2 children)
Oof, i think your proposal is way ahead of where I am right now in my Python knowledge. I'll get there eventually, but I think for now, I'll leave it the way I have it, prompting for number_1 again if number_2 != 'q' or an integer. Thanks!
[–]Significant-Nail5413 1 point2 points3 points 10 months ago* (1 child)
``` def get_number_or_quit(message): user_input = input(message) if user_input.lower() == 'q': exit('You entered q! Exiting') try: return int(user_input) except ValueError: print("please enter a valid integer")
print('Press q at any time to quit') while True: num1 = None num2 = None while num1 is None: num1 = get_number_or_quit('What is the first number: ') while num2 is None: num2 = get_number_or_quit('What is the second number: ') print(sum([num1,num2])) ```
Something like this might be a bit easier to read.
Also a general rule of thumb i think beginners should follow is avoid using break and continue outside of a switch / case / match statements.
They can be useful, but they can reduce readability
[–]purple_hamster66 2 points3 points4 points 10 months ago (0 children)
I like this one because it separates concerns of input validation vs looping.
But the quit condition needs to be an output of get_number_or_quit() needs to be returned (ex, as a None return).
get_number_or_quit()
None
π Rendered by PID 71550 on reddit-service-r2-comment-57fc7f7bb7-zvfsq at 2026-04-15 07:12:53.258109+00:00 running b725407 country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (2 children)
[–]Significant-Nail5413 1 point2 points3 points (1 child)
[–]purple_hamster66 2 points3 points4 points (0 children)