본문 바로가기
파이썬

파이썬 올바른 값을 얻을 때까지 반복 할 Try 문 가져 오기

by º기록 2020. 12. 20.
반응형

사용자가 1과 4 사이의 숫자를 입력하도록하려고합니다. 숫자가 올바른지 확인하는 코드가 있지만 숫자가 정확할 때까지 코드를 여러 번 반복하고 싶습니다. 누구든지 이것을하는 방법을 알고 있습니까? 코드는 다음과 같습니다.

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()

입력 한 예외에 대한 오류도 표시됩니다.

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

도움을 주셔서 감사합니다

 

해결 방법

 

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'

 

참조 페이지 https://stackoverflow.com/questions/2244270

 

 

반응형

댓글