Friday, March 13, 2015

Python programming basics

We will be programming using IDLE (Interactive DeveLopment Environment), software which makes it easy to write Python programs (Sweigart, 2012, p.2). IDLE was included in the installation of Python.  In Windows 8, you can find IDLE (Python GUI) in the Apps view (from the Start screen) under Python 3.2.

When we first run IDLE, we see the interactive Python shell, which we can use to input instructions for the interpreter (p. 3).  We can use the interactive shell to give instructions one line at a time, but to write a full program we need to open the file editor (Sweigart, 2015, p. 15). To access the file editor, open the File menu and select New Window.  The file editor looks like the interactive shell without the >>> prompt (p. 16).

So now it is time to code.  I start by importing the random module, which will allow me to use randInt() to generate random integers (Sweigart, 2015, p. 27).  Then I delve into object oriented programming by creating a Thing class.  I then create two instructors for the class.  Afterwards, I assign integers to several variables.  We should think of Python as pseudo-code, according to Hetland in his Instant Python crash course. In Python, variables don’t have types and don’t need to be declared. We can make variables simply by assigning to them, and we can assign to multiple variables at the same time.  After creating variables, I print them and their sum.  Then I ask the user for their name and greet them.  Then I ask the user to give a size for a list of Things and randomly generate a "size" for each Thing in the list.  Finally, I print the values of each Thing (which you may notice can be accessed directly) and tell the user goodbye.

I've included comments for more clarification.


import random

class Thing:

    # "Always remember the *self* argument" (Hetland n.d.)
    # __init__ is a pre-defined method name of the constructor.
    def __init__(self):
        self.size = []
        self.name = []

    def __init__(self,name,size):
        self.name = name
        self.size = size

a,b,c = 1,2,3 #a = 1, b = 2, c = 3
d = c + 1 #d = 3
e = random.randint(1,10) #Generates a number between 1 and 10

#If you omit the str function, you will get "TypeError: Can't convert 'int' object to str implicitly"
print(str(a) + ' + ' + str(b) + ' + ' + str(c) + ' + ' + str(d) + ' + '+ str(e) + ' = ' + str(a + b + c + d + e))

print('What is your name?')#(Sweigart p. 17)
name = str(input())
print('Hello ' + name + '!')

ask = 0

#https://docs.python.org/3.2/reference/compound_stmts.html
#https://docs.python.org/3.2/library/stdtypes.html#boolean-operations-and-or-not
while ask == 0:
    print('How many things do you want in your list? (Between 0 and 10)')
    size = input() #(Sweigart p. 24)
    size = int(size)
    #https://docs.python.org/3.2/tutorial/introduction.html#lists
    if 0 < size <= 10: #This is the same as "0 < size and size <= 10"
        ask = 1

i = 0
thingList = []
while i < size:
    print('What should the name of Thing ' + str(i) + ' be? (Give a string)')
    thingName = input()
    thingSize = random.randint(1,100) #Includes 1 and 100
    thingList.append(Thing(thingName,thingSize))
    i = i + 1

for t in thingList:
    print(t.name + ' is size ' + str(t.size))

print('Goodbye!')

Sample output:


Hetland, M. L. (n.d.). Instant Python. Retrieved from http://hetland.org/writing/instant-python.html

Sweigart, A. (2015). Invent your own computer games with Python. Retrieved from http://inventwithpython.com/inventwithpython_3rd.pdf

Sweigart, A. (2012). Making games with Python & Pygame. Retrieved from http://inventwithpython.com/makinggames.pdf


No comments:

Post a Comment