Wednesday, May 7, 2014

The story so far:

As I mentioned in the introductory post, I didn't really develop a curiosity of programming until I had begun to build and upgrade my first Windows PC.  A friend of mine, we'll call him Boots, was more than happy to lend a hand in answering every question I had concerning computers.  He was the first to introduce me to the basic concepts and syntax of Python, giving me basic lessons that included the print() and input() functions, the while and for loops, if, then statements as well as a few modules like random.

At this point, one of my first assignments was to create a script that would simulate a coin flip.  After a little thought and a fair amount of frustration, here was the completed result:

while 1 == 1:
    import random
    a = raw_input("Coin Flip? (y/n)")
    if a == "y":
        x = random.randint(1, 2)
        if x == 1:
            print ("Heads")
        if x == 2:

            print ("Tails")

While this code is rather primitive compared to most other variations of a "Coin Flip" I've seen,  I was still no less excited to have created code that actually did what I wanted it to do.

However, being so caught up as a student in Chemical Engineering, I never made much time to learn much beyond this.  I once did attempt to write a script that would solve a quadratic formula, however it would consistently crash if imaginary numbers became present.  You can see that code here:
while 1 == 1:
    import math
    print ("Quadratic Formula")
    a = int(input("a: "))
    b = int(input("b: "))
    c = int(input("c: "))
    x = (b**2-(4*a*c))
    if x < 0:
        print ("Imaginary Number(s) Present")
    print (((-b)+math.sqrt(b**2-(4*a*c)))/(2*a))

    print (((-b)-math.sqrt(b**2-(4*a*c)))/(2*a))

I've been meaning to ask some more experienced programmers as to how to fix/rebuild this code, but it unfortunately was one of many tasks that has been put on the back burner.  Maybe this summer I'll even be able to fix it myself...


I'm also realizing my little logo at the top of the page looks like it reads "Coding with JWESI"...

No comments:

Post a Comment