This post will describe the exercises and solutions for week four of Kirk Byers Python for Network Engineers.

The first exercise is the following:

[code lang=text]
I. Prompt a user to input an IP address.

Re-using some of the code from class3, exercise4–determine if the IP address is valid.

Continue prompting the user to re-input an IP address until a valid IP address is input.
[/code]

Compared to our last script we want to keep asking the user for an IP address until they supply a valid one. This means that we need a loop that can run until some condition changes. This is where While loops come in handy. We will create a Boolean variable called not_done and set this to True.

[python]
not_done = True

while not_done:
[/python]

The meaning of while not_done: is that the While loop will run as long as not_done is True.

The next step is to ask the user for an IP address. We use the built-in function input() to do this.

[python]
ip_add = input("\n\nPlease enter an IP address: ")
[/python]

We use another Boolean variable called valid_ip which is set to True until we prove that the IP address is not valid.

[python]
valid_ip = True
[/python]

We will split the IP address into four octets using the built-in function split() and use the dot as a delimiter. We use an If statement to check that we have four octets, if not the loop will continue meaning that it goes back up to the top instead of moving further down in our code.

[python]
octets = ip_add.split(".")
if len(octets) != 4:
print("\nSorry but you didn’t put in four octets. Please try again.")
continue
[/python]

The next step after that is to convert our strings in the list octets to integers. We use the try/except code from last time and raise a ValueError if we don’t succeed in the conversion and set valid_ip to False. The built-in enumerate() function is used to get an index.

[python]
for index, octet in enumerate(octets):
try:
octets[index] = int(octet)
except ValueError:
valid_ip = False
[/python]

If the IP address is not valid, we use continue once again to print a text and go back to the top of the While loop. The if not valid_ip: part checks that valid_ip isn’t True, meaning that it’s False.

[python]
if not valid_ip:
print("""\nYou entered an octet that wasn’t an integer or a blank –
that’s not going to work""")
continue
[/python]

We then reuse the same code that we had in the last script to check if the IP address is valid.

[python]
first_octet, second_octet, third_octet, fourth_octet = octets

if first_octet < 1:
valid_ip = False
elif first_octet > 223:
valid_ip = False
elif first_octet == 127:
valid_ip = False

if first_octet == 169 and second_octet == 254:
valid_ip = False

for octet in (second_octet, third_octet, fourth_octet):
if (octet < 0) or (octet > 255):
valid_ip = False
[/python]

All that is left now is to exit the script and print the IP address if it’s a valid IP, if it’s not print that it’s not a valid IP and start the While loop from the top again.

[python]
if valid_ip:
not_done = False
else:
print("\nThe IP address entered was not valid, please try again.")

print("\n\nThe IP address is valid: {}".format(ip_add))
[/python]

Running the script then looks like this.

[code lang=text]
daniel@daniel-iperf3:~/python/Week4$ python3 ip_checker_enhanced.py

Please enter an IP address: 192.168.1.1.1

Sorry but you didn't put in four octets. Please try again.

Please enter an IP address: 192.168.1

Sorry but you didn't put in four octets. Please try again.

Please enter an IP address: 169.254.0.1

The IP address entered was not valid, please try again.

Please enter an IP address: 172.16.0.1

The IP address is valid: 172.16.0.1
[/code]

I hope this has been informative and code is at Github as always.

Python – Kirk Byers Course Week 4 Part 1

3 thoughts on “Python – Kirk Byers Course Week 4 Part 1

  • September 15, 2017 at 2:55 am
    Permalink

    ”’
    if valid_ip:
    not_done = False
    else:
    print(“\nThe IP address entered was not valid, please try again.”)
    ”’
    Is continue statment missing after this else?
    If the else condition is hit here- Don’t you think the continue statement is needed for the loop to go again back up to the top?

    Reply
    • January 28, 2018 at 8:10 pm
      Permalink

      You need while loop here. Continue is required if condition did not match, alternatively you can add initial variable value at the end of while loop so it comes back to initial state.

      Reply
  • June 10, 2020 at 8:33 am
    Permalink

    If I used the “continue” statement within the “ValueError” snippet, the logic is breaking. I was forced to separate the “continue” statement. Why so?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *