Search code examples
pythonexceptionloopstry-catch

Get a Try statement to loop around until correct value obtained


I am trying to get a user to enter a number between 1 and 4. I have code to check if the number is correct but I want the code to loop around several times until the numbers is correct. Does anyone know how to do this? The code is below:

def Release():        
    try:
        print 'Please select one of the following?\nCompletion = 0\nRelease ID = 1\nVersion ID = 2\nBuild ID = 3\n'
        a = int(input("Please select the type of release required: "))
        if a == 0:
            files(a)
        elif a == 1:
            files(a)
        elif a == 2:
            files(a)
        elif a == 3:
            files(a)
        else:
            raise 'incorrect'
    except 'incorrect':    
        print 'Try Again'
    except:
        print 'Error'

Release()

I am also getting an error about the exception I have entered:

kill.py:20: DeprecationWarning: catching of string exceptions is deprecated
  except 'incorrect':
Error

Thanks for any help


Solution

  • def files(a):
        pass
    
    while True:
        try:
            i = int(input('Select: '))
            if i in range(4):
                files(i)
                break
        except:    
            pass
    
        print '\nIncorrect input, try again'