Friday, December 12, 2014

PYTHON

PYTHON

July 2013. Second month of the 8 weeks course in Beginning Java.  About this time, I am getting used to creating classes for my objects.  Some of the online students, however, were not understanding the concepts of classes and objects.

One student asked every one of our classmates about helping her understand why there were two classes in the program, via email.  I responded to her email request.

A few weeks later, she wrote me she had also asked our professor for help.  But that didn't help her either.  She ended up withdrawing from the fast-paced summer course.

Java requires strict programming rules, and writing a Java program requires a programmer to define one or more classes.  There are some programming language, however, that have no set standard for programming.  One of them is Python.

During my last week, this week, of tutoring in the Networking Lab, a student showed me his Python program.  In it, he had a series of definition of function followed by a call to that same function.  At the end of his program he had a call to the main function.  His program worked, but I told him his program was the first program I’ve seen with such a weird style of programming.

Here is one way of programming Python using good programming practices.

#MAIN FUNCTION: opens file and call functions to write and display file.
def main():
    f = open('testfile2014.txt', 'a')
    writeFile(f)
    displayFile(f)

#WRITEFILE FUNCTION: writes a text into file.
def writeFile(f):
    f.write('\n')
    f.write(input("Type a text to enter in file:\n"))
    f.close()

#DISPLAYFILE FUNCTION: reads a file and display it on screen.
def displayFile(f):
    with open('testfile2014.txt', 'r') as f:
        read_data = f.read()
    f.closed
    print(read_data)

#MAIN PROGRAM
main()

Happy Twelve Days Before Christmas!

John Sindayen

No comments:

Post a Comment