Assignment 11 In Python identify a correctly formed IP addr

Assignment # 11: In Python identify a correctly formed IP address

write code that tests any given IP address for correctness.

Here is the rule:

There should be four numbers separated by periods.

Each number should be between 0 and 255.

In the assignment provided examples of correct IP addresses

Some correctly formed ip addresses:

27.123.128.44

183.81.128.189

202.170.47.250

210.7.0.1

Your code should analyse a given IP address and determine if it is legal.

Solution

ip = \"256.7.0\"
l = ip.split(\".\")                       # split the ip string based on dot
flag = True                               # set the flag true
if(len(l)==4):                           # check if ip has 4 parts
   for i in l:
       if i.isdigit():                   # check each part is a digit
           if int(i) < -1 or int(i) > 256:   # digit should be between 0-256
               flag = False           # if not make flag false
               break
       else:
           flag = False               # if not a digit make flag false
           break
else:                                   # if it does not have 4 parts make flag false
   flag = False

if(flag):
   print \"Valid IP\"
else:
   print \"Invalid IP\"

# sample output

# 257.1.0.1

# Invalid IP

#256.1.0.1

# valid IP

Assignment # 11: In Python identify a correctly formed IP address write code that tests any given IP address for correctness. Here is the rule: There should be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site