Search This Blog

Friday, March 20, 2015

Cisco and Python

I attended the Cisco Geekfest in Austin a few weeks ago since we are pretty close. The opening introduction on day 2 had Cisco preaching we, as engineers need to know some basic programming.  The code they were showing off for us to know was python and some java.  I have personally dabbled a little in Java but to me that programming language is a mess and got difficult quick.  I think part of the problem was me not being able to draw out the loops.

Python on the on the other hand I started picking up two days ago.  I'm already to the point where I can write a while or for loop with little to no issues.  Now I haven't done anything crazy yet since I am still in the crawl phase but it's coming along well.  For those of you that don't know any programming like me, I suggest Python as a first language as it is extremely forgiving and tells you exactly what the problem is.  Look below for an example on something I screwed up in my code.

c:\Python34>python calculator.py
Traceback (most recent call last):
  File "calculator.py", line 84, in <module>
    main()
  File "calculator.py", line 69, in main
    print(longstring)
NameError: name 'longstring' is not defined

As you can see, I forgot to remove a variable called "longstring".  I put some ASCII art in my program and decided to pull it out as it was making it hard for me to read some stuff half way down the program.  I made a basic calculator that does your standard 4 math functions as well as calculate areas the radius of shapes.  The CBT Nuggets course helped me quite the bit but a YouTube channel with a guy named onestopprogramming really propelled me as he explains every little detail and the "why" as well as the "how".  Their website also goes a long way and provides other little practice examples with the full code following if you need help or want to see the solution.

I can see how programming could be useful for us engineers but at the same time, keep it to the basics unless you have a desire to learn something new.  I personally found programming to be fun as you frequently encounter problems and need to find the solution.  With VoIP, this also happens but is usually a long complex drawn out process unless you just fat fingered a route pattern or screwed up a CSS.  Anyways, just felt that if you didn't already know, some basic programming stuff is coming our way in the years to come and you might want to at least be able to read and understand whats going on behind the scenes.

...I figured I would post my calculator program I built.  90% of this is all my work while the other 10% I got help from the videos when I initially started off since it was part of the learning experience.  In fact, I was never told nor shown how to make an advanced menu that spits you back out to the main menu when you completed a task(Go me...).  Below is the code, if you can figure it out then you are already ahead of the game (Not that its hard).  I included everything here and a copy/paste in Python 3.X will work great.

# Returns the sum of num1 and num2
def add(num1, num2):
    return num1 + num2

 #Returns the result of subtracting num1 - num2
def sub(num1, num2):
    return num1 - num2

 #Returns the result of multiplying num1 * num2
def mul(num1, num2):
    return num1 * num2

 # Returns the result of dividing num1 / num2
def div(num1, num2):
    return num1 / num2

 # This is the basic math function that is called after the main menu (option 1 on the menu)   
def basicMath():
    operation = input('What do you want to do? (+,-,*,/):')
    if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        #invalid operation
        print('You must enter a valid operation, HAULTING!!!')
    else:
        var1 = int(input('Enter num1: '))
        var2 = int(input('Enter num2: '))
        if(operation == '+'):
            print(add(var1, var2))
            main()
        elif(operation == '-'):
            print(sub(var1, var2))
            main()
        elif(operation == '*'):
            print(mul(var1, var2))
            main()
        else:
            print(div(var1, var2))
            main()

 #This is the area math function that is called after the main menu (option 2 on the menu)
def areaMath():
    print('Choose an option:')
    print('1: Area of a Circle')
    print('2: Area of a Rectangle')
    print('3: Area of a Square')
    print('4: area of a Triangle')
    operation = input('What do you want to do?:')
   
    if(operation == '1'):
        circle = int(input('Enter the radius of the circle: '))
        print('Your area based off of the circle number input is:', 3.14 * circle ** 2)
        main()
    elif(operation == '2'):
        rectangleLen = int(input('Enter the length of the rectangle: '))
        rectangleWid = int(input('Enter the width of the rectangle: '))
        print('The area of the rectangle based off of your input is:', rectangleLen* rectangleWid)
        main()
    elif(operation == '3'):
        square = int(input('Enter the length of the side of the square: '))
        print('The area of the square based off of your input is: ', square ** 2)
        main()
    else:
        triangleBase = int(input('Enter the base length of the triangle: '))
        triangleHeight = int(input('Enter the height of the triangle: '))
        print('The area of the triangle based off of your input is: ', (triangleBase / 2) * triangleHeight)
        main()
           
 #This is the main function of the program
def main():
    print('')
    print('Please choose an option or press enter to exit')
    print('1: Basic Math')
    print('2: Area Calculations (i.e. diameter of circles or areas of squares)')
    menu = input('Please choose an option now: ')
    if(menu == '1'):
        basicMath()
    elif(menu == '2'):
        areaMath()
    else:
        print('')
        print('You are recieving this message because you either pressed "enter" or did not enter a 1 or 2. Haulting Program now!')
   
main()

No comments:

Post a Comment