This post will describe the exercises and solutions for week four of Kirk Byers Python for Network Engineers.
The first exercise is the following:
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.
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.
not_done = True while not_done:
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.
ip_add = input("\n\nPlease enter an IP address: ")
We use another Boolean variable called valid_ip which is set to True until we prove that the IP address is not valid.
valid_ip = True
We will split Continue reading