I want to get a list IP addresses from the user that match with my regex using a for loop. This is my code.
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")
This works like this,
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
But I want to repeat the particular iteration which fails the if condition, so that the prompt for the input works as expected. Like this,
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:
Any help would be appreciated. Thanks in advance.