Stuck on error in Python 3 (exceptions, loop)

So what I'm trying to do here is that if you enter a string instead of an integer, go back to the beginning

But for some reason the program just stops when you put the line

while True: try: print("Will select a random number between selected limits: 'x' and 'y'") x = int(input("x = ")) except ValueError: print("Please enter a number") break 
+4
source share
2 answers
 while True: try: print("Will select a random number between selected limits: 'x' and 'y'") x = int(input("x = ")) y = int(input("y = ")) except ValueError: print("Please enter a number") # don't break here, let loop repeat else: break # only break when there is no error 
+5
source

Even shorter, one line;):

 while True: try: print("Will select a random number between selected limits: 'x' and 'y'") x = int(input("x = ")) y = int(input("y = ")) break # only break when there is no error except ValueError: print("Please enter a number") # don't break here, let loop repeat 
+1
source

Source: https://habr.com/ru/post/1482171/


All Articles