This script checks three VLAN numbers, so the same test is written three times:
data_vlan = 20 voice_vlan = 60 guest_vlan = 5000 print(data_vlan >= 1 and data_vlan <= 4094) print(voice_vlan >= 1 and voice_vlan <= 4094) print(guest_vlan >= 1 and guest_vlan <= 4094)A function gives a name to these lines.
You define them once withdef, then you use the name each time:
Figure 1 - A Function Is Written Once and Used by Its Name
The lines of the function start with four spaces like with
if.In the lab, the function
is_access_portis already written, but the script does not use it.
Use it for the three ports, then make it accept the ports up to 46 to get the flag.Answer the question below
Enter the flag you obtained.
The variable between the parentheses is a parameter.
It gets its value when you call the function:
Figure 2 - The Call Gives a Value to the Parameter
A function can have several parameters, with a comma between them:
def in_range(number, last): return number >= 1 and number <= last print(in_range(30, 24)) # Output: False print(in_range(30, 48)) # Output: TrueIn the lab, the function
port_namealways gives a port that starts withGi0/.
Add a parametermodulefor this first number and use the function to printGi1/5to get the flag.Answer the question below
Enter the flag you obtained.
A function can give a value back to the line that called it.
You write this value afterreturn:
Figure 3 - return Gives the Result Back to the Call
A variable created inside a function does not exist outside of it.
returnis the way to get a value out of the function.In the lab, the script prints
Nonebecause the functionport_namegives nothing back.
Make it return the name of the port to get the flag.Answer the question below
Enter the flag you obtained.
A script often has several functions, with one job each.
The main program at the end calls them:
Figure 4 - The Functions, Then the Main Program
Python must read a function before you call it, so the functions come first.
A function is never mandatory, but a script with functions is easier to read and to change.In the lab, put the test in a function named
is_validand the twoprintlines in a function namedset_vlan.
The script must print the same config as before to get the flag.Answer the question below
Enter the flag you obtained.