Apr-09-2019, 12:37 PM
(This post was last modified: Apr-09-2019, 12:44 PM by souprqtpie.)
(Apr-09-2019, 12:08 PM)ichabod801 Wrote: There are a number of ways you can deal with this. You could do two separate inputs to get the different numbers. You could be explicit about needing a space. You could replace other common delimiters with spaces before splitting (input('Enter two numbers: ').replace(',', ' ').split()
). You could write a regular expression matching valid numbers, and use findall to get a list of valid numbers in the input.
If you want to give multiple chances to correct the input, a while True loop is often used:
while True: values = input('Enter two numbers: ') try: # to convert to integer except ValueError: print('Invalid input') else: breakInstead of (or in addition to) the try/except block, you might check for a valid range or other issues with if/else, again breaking if the input is valid.
thanks for the answer!
could you explain the replace bit to me in a little more detail please showing how it works. ( I am trying to keep both numbers on the same input line). I've done the while true loop before, but I didn't know how to get it to measure both numbers at once without making a separate loop for each one