My Python program (no GUI, just text-based and will be run through the terminal) has a list of options, 1-4 and 0. I have a list of these numbers as strings, and the while loop starts with
while response not in ['1', '2', '3', '4', '0']:
What I am wanting is for the while loop to continue indefinitely until the user inputs a valid response from the list. I have a prompt message for if they do not input a valid response, that says "Invalid response. Please double-check and enter the number for the desired option." However, if they continually input an invalid response, it continues to make a new line with that same error message every time, which leads to multiple lines of the same text that looks like this:
Type number for desired option, then press enter --> 5
Invalid response. Please double-check and enter the number for the desired option. --> 5
Invalid response. Please double-check and enter the number for the desired option. --> 5
Invalid response. Please double-check and enter the number for the desired option. --> 5
Invalid response. Please double-check and enter the number for the desired option. --> 5
I am wanting it to just overwrite that same line, so that every time they enter something invalid, it just basically 'resets' the line the same way it would sort of work with the print statement and the carriage return, so it would look like this, even after inputting 5 100 times in a row:
Type number for desired option, then press enter --> 5
Invalid response. Please double-check and enter the number for the desired option. --> 5
Is there a roundabout way I can achieve this? I know you can use carriage return with print() which I have tried, but that doesn't work with input() since input() doesn't have that functionality.
I have tried printing the invalid message response with end="\r" and then having the response variable response= input() on the next line. This doesn't work because it still repeats the line. I also tried nesting a print statement with the end parameter inside of an input statement, but that didn't work either.