Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a Try statement to loop around until correct value obtained

Tags:

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

like image 507
chrisg Avatar asked Feb 11 '10 12:02

chrisg


People also ask

Is there a do until loop in Python?

There is no do-while loop in Python.

How you can come out of the loop when a condition is satisfied?

I hope you could help me. I'm not sure to understand the detail of your code, but you can break out of a loop using the break keyword. For example you can do if ...: break , replacing ... with the condition that will allow to exit the loop. If you keep adding to count it will never break out.


1 Answers

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' 
like image 67
Johannes Charra Avatar answered Oct 29 '22 16:10

Johannes Charra