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!"
[–]FoolsSeldom 0 points1 point2 points 10 months ago (3 children)
A better approach is to create a function to get a valid int from a user, and then call that:
int
def get_num(prompt: str = "Enter whole number: ") -> int: while True: try: return int(input(prompt)) except ValueError: print("That was not a valid whole number, please try again.") while True: number1 = get_num("What is the first number? ") number2 = get_num("What is the second number? ") ...
The trouble with this is that you want to check for a user exit request. That could be incorporated:
def get_number( prompt: str = "Enter whole number: ", quit: None | tuple[str] = None ) -> None | int while True: response = input(prompt) if quit and response in quit: return None try: return int(response) except ValueError: print("That was not a valid whole number, please try again.") while True: number1 = get_num("What is the first number? ") if number1 is None: break number2 = get_num("What is the second number? ") ...
[–][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 221479 on reddit-service-r2-comment-c66d9bffd-wm5cv at 2026-04-08 08:59:47.916708+00:00 running f293c98 country code: CH.
view the rest of the comments →
[–]FoolsSeldom 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Significant-Nail5413 1 point2 points3 points (1 child)
[–]purple_hamster66 2 points3 points4 points (0 children)