Home > database >  Get the input again if wrong by using input validation
Get the input again if wrong by using input validation

Time:07-06

Here I need to get 10 IPv4 addresses and check them using a validator function if the IPv4 address is invalid it should again ask for that input alone which was invalid out of 10 from the user. If the IPv4 address is valid no problem it goes to the converter function and runs smoothly.

Here I tried this but this goes on an infinite loop

while True:
      ipv4 = input("Enter 10 IPv4 Address: ").split(" ")
      for i in range(10):
        if validator(ipv4[i]):
          converter(ipv4[i])
        else:
          print("Invalid IPv4 Address")
          while True:
            ip = input("Enter the Valid IPv4 Address:")
            if validator(ip):
              converter(ip)
              break
      break

CodePudding user response:

make these changes in the second while loop:

while True:
    print(f"{ipv4[i]} is an Invalid IPv4 Address, add valid IPv4 Address")
    ipv4[i] = input("Enter the Valid IPv4 Address:")
    if validator(ipv4[i]):
        converter(ipv4[i])
        break
  • Related