Fundamentals of Network Automation Using Python (def not working)

This course: Fundamentals of Network Automation Using Python, offers scripts that work on my ubuntu/python 3.10.4 until we get to use the def() function. The scripts after these lessons do not output errors, or print (), or anything at all. I think the scripts need to be revised/updated/re-tested. Please help. I can run the scripts, but they return nothing. Not even an error.

Hello!

Can you provide more information on the specific tasks or video or lab?

Conditionals & Functions - Lab Task
the script does not return any output, no error nothing. I am running ubuntu 3.10.4
If I remove def(), then I can print and do things. Basically def does not work on that script. Or any other script after the lesson. I am stocked on that lesson.

Did you try this?

def device_ip(temp):
dict1 = {“hostname”: “R1”, “OS”: “IOS-XE”, “mgmt-ip”: “10.1.1.1”}
dict2 = {“hostname”: “R2”, “OS”: “IOS-XR”, “mgmt-ip”: “10.1.1.2”}
if temp in dict1[“OS”] == “IOS-XE”:
print(“The management IP of”, dict1[“hostname”], “is”, dict1[“mgmt-ip”])
elif temp in dict2[“OS”] == “IOS-XR”:
print(“The management IP of”, dict2[“hostname”], “is”, dict2[“mgmt-ip”])
else:
print(“This device has an unknown image”)

Make sure indentation is correct

1 Like

Hello Rohit, thanks for getting back to me. Yes I am using Atom editor and ubuntu linux to run python scripts as you type them on the lessons. Everything was working fine until we got to def() that returns no output at all. Not even an error:

atom device_ip.py

def device_ip(temp):
dict1 = {“hostname” : “R1”, “OS”: “IOS-XE”, “mgmt-ip”: “10.1.1.1”}
dict2 = {“hostname” : “R1”, “OS”: “IOS-XR”, “mgmt-ip”: “10.1.1.@”}
if temp in dict1[“OS”] == “IOS-XE”:
print(“The management IP of”, dict1[“hostname”], “is”, dict1[“mgmt-ip”])
elif temp in dict2[“OS”] == “IOS-XR”:
print(“The management IP of”, dict2[“hostname”], “is”, dict2[“mgmt-ip”])
else:
print(“This device has an unknown image”)

1 Like

Hello Rohit, are you finding an answer?

My indentation is correct and the functions do not work. Can you look into that, please?

The above script is correct. It will print just the first line (“The management IP of R1 is 10.1.1.1”). Also this is not a function so it does not need to be called inside a For Loop. The condition is met using the if statement which says if dictionary dict1 OS key matches the value of IOS-XE then print.

Hey there,

I have not taken Rohit’s course and do not want to step on any toes, but I saw several reasons why you might not be getting the results you wanted from the script and wanted to provide some info.

  • This def command is defining a function.
  • You have no function call in the code, so the function never runs
  • The portion in parentheses is called an “Argument”. This allows you to insert information when performing a function call. As no function is called, the Argument does nothing.

def device_ip(temp):
dict1 = {“hostname” : “R1”, “OS”: “IOS-XE”, “mgmt-ip”: “10.1.1.1”}
dict2 = {“hostname” : “R1”, “OS”: “IOS-XR”, “mgmt-ip”: “10.1.1.@”}

  • This “temp” Argument is very weird to me, it seems to serve no purpose, but the structure of the IF statement is odd as well. It seems like this wants a user to input either IOS-XE or IOS-XR and then use the function call to pass that on to the IF statement, but if this is the case, you don’t need to define the IOS-XE in a dictionary. It could literally just be re-written as “if temp == “IOS-XE”:”, but would still require a function call and something else to define IOS-XE or IOS-XR.

if temp in dict1[“OS”] == “IOS-XE”:
print(“The management IP of”, dict1[“hostname”], “is”, dict1[“mgmt-ip”])
elif temp in dict2[“OS”] == “IOS-XR”:
print(“The management IP of”, dict2[“hostname”], “is”, dict2[“mgmt-ip”])
else:
print(“This device has an unknown image”)

The reason the code runs without the def statement is because python will execute code from top to bottom when run, but functions only run when specifically called. This is usually performed by just typing the function name and parentheses after it. When using Arguments, you insert them in to the parentheses.

Example:
device_IP(“IOS-XE”)

Hope you find the above helpful.