Outreach program at UofC with Mark Oreglia.
Basic computer programming:
- concepts
- simple program
- cannon game
- plotting
core concepts of writing computer programs--variables, decisions, loops, functions, and objects--
Installing Python and PyGame
on a Mac OS X 10.6
Couple of attempts failed (system python, python3)
MacPort worked after some tweaks.
If you don't have MacPort (aka Darwinport), check
http://www.macports.org/install.php and use the .dmg installation package.
Once you have MacPort tart installing python2.6, numpy and py-game:
sudo port install python2.6
sudo port install py26-numpy
sudo port install py26-game
# probably will end with an error:
Error: Target org.macports.build returned: shell command " cd "/opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_ports_python_py26-game/work/pygame-1.9.1release" && /opt/local/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 setup.py --no-user-cfg build " returned error 1
...
src/pypm.c:136:22: error: porttime.h: No such file or directory
...
error: command '/usr/bin/gcc-4.2' failed with exit status 1
Error: Status 1 encountered during processing.
Before reporting a bug, first run the command again with the -d flag to get complete output.
Edit the Setup file to remove pypm (comment out the line compiling pypm.c) and retry the build. This time it should work.
pushd /opt/local/var/macports/build/
cd _opt_local_var_macports_sources_rsync.macports.org_release_ports_python_py26-game/work/pygame-1.9.1release/
sudo port install py26-game
# should complete succesfully
popd
Windows
Download and install the followings:
Detailed instructions are at
http://pygame.org/install.html
Packages to install:
Editor
To test:
VPython
Alternatively you can use Portable Python:
http://www.portablepython.com/
The recommended editor is SciTE
The game
Evaluating some games:
This is a simple cannon game:
http://shellfire.sourceforge.net/
/opt/local/bin/python2.6 main.py
Counting words
def read_text(filename):
f = open(filename)
my_text = f.readlines().split()
return my_text
def count_word(text, word):
counter = 0
for w in text:
if w==word:
counter += 1
return counter
def find_important_words(text):
word_list = []
text_summary = []
for w in text:
if len(w)>2 and w not word_list:
word_list.append(w)
occurrences = count_word(text, w)
text_summary.append((w, occurrences))
return text_summary
def main():
EMPTY_LIST = (0, 0, 0)
file_names = ['text1', 'text2', 'text3']
for fn in file_names:
ft = read_text(fn)
text_index = find_important_words(ft)
for i in text_index:
w = i[0]
occ = i[1]
if not i_index.has_key(w):
i_index[w] = EMPTY_LIST
i_index[w][j] = occ
# i_index[w][j] = occ/len(ft)
# print table
print file_names
for w, line in i_index:
print "%s: %s" % (w, line)
References and links
--
MarcoMambelli - 28 Jun 2010