2017-12-13 2 views
2

for 루프를 사용하여 my regex와 일치하는 사용자의 목록 IP 주소를 가져 오려고합니다. 이것은 내 코드입니다.필요한 정규식이 입력에서 일치하지 않는 경우 파이썬의 루프에서 반복을 반복하는 방법

count=input('Enter number of machines:') 
usrip=re.compile("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}") 
for i in range(1,count): 
     user=raw_input('Enter the ip_address of machine '+str(i)+':') 
     if (usrip.match(user)): 
      mac_list.append(user) 
     else: 
      print("Enter a valid user name and ip address") 

이이처럼 작동

Enter number of machines: 3 
Enter the ip_address of machine 1: not an ip 
Enter a valid user name and ip address 
Enter the ip_address of machine 2: 12345 
Enter a valid user name and ip address 

는하지만 예상대로 입력에 대한 프롬프트가 작동되도록 경우 조건을 실패 특정 반복을 반복합니다. 이와 같이

Enter number of machines: 3 
Enter the ip_address of machine 1: not an ip 
Enter a valid user name and ip address 
Enter the ip_address of machine 1: 12345 
Enter a valid user name and ip address 
Enter the ip_address of machine 1: 192.168.1.1 
Enter the ip_address of machine 2: 

어떤 도움을 주시면 감사하겠습니다. 미리 감사드립니다.

+0

내가이 중복 생각하지 않습니다 시도하지만,이 솔루션은 다른 문제의 하나와 유사한 : 무한 루프를 확인, IPS의 사용자를 묻는에 추가 유효하다면 목록을 작성하고 충분할 때 종료하십시오. – L3viathan

답변

0

count=input('Enter number of machines:') 
usrip=re.compile("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}") 
for i in range(1,int(count)): 

     user=input('Enter the ip_address of machine '+str(i)+':') 
     while not (usrip.match(user)): 
      print("Enter a valid user name and ip address") 

      user=input('Enter the ip_address of machine '+str(i)+':') 

     if (usrip.match(user)): 
      mac_list.append(user) 
관련 문제